无法反序列化响应

时间:2017-02-06 15:07:44

标签: c# .net botframework

我搜索并找到了这个帖子:
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"       姓名:" Bot"

     

message.Recipient       {} Microsoft.Bot.Connector.ChannelAccount           Id:"默认用户"           姓名:"用户"

     

message.Conversation {Microsoft.Bot.Connector.ConversationAccount}       Id:" a0m69i29gih3kjf3mc"       IsGroup:null       姓名:null

     

message.Text ="您好,这是一个通知&#34 ;;                       message.Locale =" en-Us&#34 ;;

我在尝试发布时遇到此错误:

  

无法反序列化响应。

e.InnerException

  

{"解析值时遇到意外的字符:<。路径'',   第0行,第0位。"}       数据:{System.Collections.ListDictionaryInternal}       HResult:-2146233088       HelpLink:null       InnerException:null       LineNumber:0       LinePosition:0       消息:"解析值时遇到意外的字符:<。路径'',第0行,第0位。"       路径:""       资料来源:" Newtonsoft.Json"       StackTrace:"在Newtonsoft.Json.JsonTextReader.ParseValue()\ r \ n在Newtonsoft.Json.JsonTextReader.Read()\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader   reader,JsonContract contract,Boolean hasConverter)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader   reader,Type objectType,Boolean checkAdditionalContent)\ r \ n at   Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader阅读器,   输入objectType)\ r \ n at   Microsoft.Rest.Serialization.SafeJsonConvert.DeserializeObject [T](字符串   json,JsonSerializerSettings设置)\ r \ n at   Microsoft.Bot.Connector.Conversations.d__6.MoveNext()"       TargetSite:{Boolean 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: "some-email@hotmail.com"
    fromId: "49gjgijl7a4inc821c"
    fromName: ""
    serviceUrl: "http://localhost:3979"
    toId: "default-user"
    toName: ""

我正在为名称和名称明确添加本地测试(参见代码)。

1 个答案:

答案 0 :(得分:1)

此处您有IsGroupName属性设置为null:

  

message.Conversation {Microsoft.Bot.Connector.ConversationAccount} Id:   “a0m69i29gih3kjf3mc”IsGroup:null名称:null

尝试为这些属性赋值。

此外也可能发生错误:

if (botAccount.Name == "")
  botAccount.Name = "Bot";
if (userAccount.Name == "")
  userAccount.Name = "User";  

如果string == ""个对象(nullbotAccount.Name = null),则userAccount.Name = null检查字符串的空白将失败。
请尝试使用string.IsNullOrEmpty()

if (string.IsNullOrEmpty(botAccount.Name))
  botAccount.Name = "Bot";
if (string.IsNullOrEmpty(userAccount.Name))
  userAccount.Name = "User";