【问题标题】:Unable to deserialize the response无法反序列化响应
【发布时间】:2017-02-06 15:07:43
【问题描述】:

我已经搜索并找到了这个帖子:
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} 编号:“49gjgijl7a4inc821c” 名称:“机器人”

message.Recipient {Microsoft.Bot.Connector.ChannelAccount} ID:“默认用户” 名称:“用户”

message.Conversation {Microsoft.Bot.Connector.ConversationAccount} 编号:“a0m69i29gih3kjf3mc” 是组:空 名称:空

message.Text = "你好,这是一条通知"; message.Locale = "en-Us";

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

无法反序列化响应。

e.InnerException

{"解析值时遇到意外字符:<.path h stacktrace: newtonsoft.json.jsontextreader.parsevalue newtonsoft.json.jsontextreader.read newtonsoft.json.serialization.jsonserializerinternalreader.readfortype hasconverter at newtonsoft.json.serialization.jsonserializerinternalreader.deserialize objecttype checkadditionalcontent newtonsoft.json.jsonserializer.deserializeinternal microsoft.rest.serialization.safejsonconvert.deserializeobject json microsoft.bot.connector.conversations.d__6.movenext 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: ""

我正在为本地测试显式添加 name 和 from name(请参阅代码)。

【问题讨论】:

  • 你能提供正在反序列化的文本吗?
  • 是的。我编辑了我的帖子并添加了我的对象内容。
  • 请求的状态码是什么(任何错误?)以及jsonContent-变量中的实际内容是什么(我假设是一些 html/xml 响应)?
  • 响应中一切正常。发布异步时会出现问题......
  • 你能把jsonContent的值贴出来吗?与在 Visual Studio 中检查的完全相同。

标签: c# .net botframework


【解决方案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";  

如果是null 对象(当botAccount.Name = nulluserAccount.Name = null 时),使用string == "" 检查字符串是否为空将失败。
尝试改用string.IsNullOrEmpty()

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

【讨论】:

  • 当有关频道的信息保存为空时,组属性是否设置为空。还有名字。解析响应时等于“”。所以这不是问题....可能是另一个想法?
猜你喜欢
  • 2021-05-14
  • 2020-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多