我已经搜查,发现这个线程: Microsoft.Bot ConnectorClient SendMessage Unable to deserialize the response 但没有代码和解释。
到目前为止,我有这样的:
var obj = model[i];
var userAccount = new ChannelAccount(obj.toId, obj.toName);
var botAccount = new ChannelAccount(obj.fromId, obj.fromName);
var connector = new ConnectorClient(new Uri(obj.serviceUrl));
IMessageActivity message = Activity.CreateMessageActivity();
if (!string.IsNullOrEmpty(obj.conversationId) && !string.IsNullOrEmpty(obj.channelId))
{
message.ChannelId = obj.channelId;
}
else
{
obj.conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
}
if (botAccount.Name == "")
botAccount.Name = "Bot";
if (userAccount.Name == "")
userAccount.Name = "User";
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: obj.conversationId);
message.Text = "Hello, this is a notification";
message.Locale = "en-Us";
await connector.Conversations.SendToConversationAsync((Activity)message);
我的消息对象:
{} Microsoft.Bot.Connector.ChannelAccount ID: “49gjgijl7a4inc821c” 名称: “博特”
message.Recipient {} Microsoft.Bot.Connector.ChannelAccount ID: “默认用户” 名称: “用户”
message.Conversation {} Microsoft.Bot.Connector.ConversationAccount ID: “a0m69i29gih3kjf3mc” IsGroup:空名称:空
message.Text =“您好,这是一个通知”; message.Locale = “EN-US”;
尝试后,当我得到这个错误:
无法反序列化的响应。
e.InnerException
数据:{ “<路径 '',第0行,位置0非预期的字符在解析值遇到”}:{System.Collections.ListDictionaryInternal} HRESULT:-2146233088 HELPLINK:空的InnerException:空LineNumber上:0 LinePosition:0消息: “意外的字符遇到在解析值:<路径‘’,第0行,位置0”。路径: “” 来源: “Newtonsoft.Json” 堆栈跟踪:“在Newtonsoft.Json.JsonTextReader.ParseValue(个)\ r \ n在Newtonsoft.Json.JsonTextReader.Read(个)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalReader .ReadForType(JsonReader读卡器,JsonContract合同,布尔hasConverter个)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader读卡器,类型的objectType,布尔checkAdditionalContent个)\ r \ n在Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader阅读器,类型的objectType个)\ r \ n在Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject [T](字符串JSON,JsonSerializerSettings设置)\ r \ n在Microsoft.Bot.Connector.Conversations.d__6.MoveNext()” TargetSite: {布尔ParseValue()}
任何帮助吗?
编辑:这是我如何反序列化对象。
HttpContent requestContent = Request.Content;
string jsonContent = requestContent.ReadAsStringAsync().Result;
List<ConversationModel> model = new List<ConversationModel>();
try {
model = JsonConvert.DeserializeObject<List<ConversationModel>>(jsonContent);
这是我的对象内容:
channelId: "emulator"
conversationId: "a0m69i29gih3kjf3mc"
email: "[email protected]"
fromId: "49gjgijl7a4inc821c"
fromName: ""
serviceUrl: "http://localhost:3979"
toId: "default-user"
toName: ""
我加入到名字和名称,明确本地测试(查看代码)。
在这里,你有被设置为null IsGroup
和Name
属性:
message.Conversation {} Microsoft.Bot.Connector.ConversationAccount ID: “a0m69i29gih3kjf3mc” IsGroup:空名称:空
先给值的属性。
还不慎就可能在这里也发生:
if (botAccount.Name == "")
botAccount.Name = "Bot";
if (userAccount.Name == "")
userAccount.Name = "User";
检查与string == ""
串的空虚将在null
对象的情况下,失败(当botAccount.Name = null
或userAccount.Name = null
)。
尝试使用string.IsNullOrEmpty()
代替:
if (string.IsNullOrEmpty(botAccount.Name))
botAccount.Name = "Bot";
if (string.IsNullOrEmpty(userAccount.Name))
userAccount.Name = "User";