2017-06-14 52 views
1

StackOverflow社区!JSON串行器错误与BotFramework LUIS

我有一个聊天机器人,并集成了LUIS.ai,使它更聪明。其中一个对话即将预定与主管(教师)的约会 一切工作都很好,字面上相同的代码。几个小时前,我遇到了一些奇怪的错误。

Exception: Type 'Newtonsoft.Json.Linq.JArray' in Assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' is not marked as serializable. 

如何重现错误?

如果用户的输入中缺少两个实体(教师和日期),则它会运行FINE,bot将生成表单,询问缺少的输入并显示建议的会议时间。

如果其中一个实体缺少输入,它将生成一个表单并要求提供缺失的日期或教师实体,并提出建议的会议时间。

如果用户的输入既包含实体:教师与日期也一样,然后我得到的错误。

这里是我WebApiConfig类:

public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Json settings 
      config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; 
      config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented; 
      JsonConvert.DefaultSettings =() => new JsonSerializerSettings() 
      { 
       ContractResolver = new CamelCasePropertyNamesContractResolver(), 
       Formatting = Newtonsoft.Json.Formatting.Indented, 
       NullValueHandling = NullValueHandling.Ignore, 
      }; 

      // Web API configuration and services 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
     } 
    } 

当我试图从用户的话语,这是一种builtin.dateTimeV2的获得一个实体,我只遇到此错误。

这种异步方法被称为:

//From the LUIS.AI language model the entities 
    private const string EntityMeetingDate = "MeetingDate"; 
    private const string EntityTeacher = "Teacher"; 

    [LuisIntent("BookSupervision")] 
public async Task BookAppointment(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result) 
{ 
    var message = await activity; 
    await context.PostAsync($"I am analysing your message: '{message.Text}'..."); 

    var meetingsQuery = new MeetingsQuery(); 

    EntityRecommendation teacherEntityRecommendation; 
    EntityRecommendation dateEntityRecommendation; 

    if (result.TryFindEntity(EntityTeacher, out teacherEntityRecommendation)) 
    { 
     teacherEntityRecommendation.Type = "Name"; 
    } 
    if (result.TryFindEntity(EntityMeetingDate, out dateEntityRecommendation)) 
    { 
     dateEntityRecommendation.Type = "Date"; 
    } 

    var meetingsFormDialog = new FormDialog<MeetingsQuery>(meetingsQuery, this.BuildMeetingsForm, FormOptions.PromptInStart, result.Entities); 
    context.Call(meetingsFormDialog, this.ResumeAfterMeetingsFormDialog); 

} 

其它方法构建的一种形式:

private IForm<MeetingsQuery> BuildMeetingsForm() 
{ 
    OnCompletionAsyncDelegate<MeetingsQuery> processMeetingsSearch = async (context, state) => 
    { 
     var message = "Searching for supervision slots"; 
     if (!string.IsNullOrEmpty(state.Date)) 
     { 
      message += $" at {state.Date}..."; 
     } 
     else if (!string.IsNullOrEmpty(state.Name)) 
     { 
      message += $" with professor {state.Name}..."; 
     } 
     await context.PostAsync(message); 
    }; 

    return new FormBuilder<MeetingsQuery>() 
     .Field(nameof(MeetingsQuery.Date), (state) => string.IsNullOrEmpty(state.Date)) 
     .Field(nameof(MeetingsQuery.Name), (state) => string.IsNullOrEmpty(state.Name)) 
     .OnCompletion(processMeetingsSearch) 
     .Build(); 
} 

private async Task ResumeAfterMeetingsFormDialog(IDialogContext context, IAwaitable<MeetingsQuery> result) 
{ 
try 
{ 
    var searchQuery = await result; 

    var meetings = await this.GetMeetingsAsync(searchQuery); 

    await context.PostAsync($"I found {meetings.Count()} available slots:"); 

    var resultMessage = context.MakeMessage(); 
    resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel; 
    resultMessage.Attachments = new List<Attachment>(); 

    foreach (var meeting in meetings) 
    { 
     HeroCard heroCard = new HeroCard() 
     { 
      Title = meeting.Teacher, 
      Subtitle = meeting.Location, 
      Text = meeting.DateTime, 
      Images = new List<CardImage>() 
      { 
       new CardImage() {Url = meeting.Image} 
      }, 
      Buttons = new List<CardAction>() 
      { 
       new CardAction() 
       { 
        Title = "Book Appointment", 
        Type = ActionTypes.OpenUrl, 
        Value = $"https://www.bing.com/search?q=easj+roskilde+" + HttpUtility.UrlEncode(meeting.Location) 
       } 
      } 
     }; 

     resultMessage.Attachments.Add(heroCard.ToAttachment()); 
    } 

    await context.PostAsync(resultMessage); 
} 
catch (FormCanceledException ex) 
{ 
    string reply; 

    if (ex.InnerException == null) 
    { 
     reply = "You have canceled the operation."; 
    } 
    else 
    { 
     reply = $"Oops! Something went wrong :(Technical Details: {ex.InnerException.Message}"; 
    } 

    await context.PostAsync(reply); 
} 
finally 
{ 
    context.Wait(DeconstructionOfDialog); 
} 
} 


private async Task<IEnumerable<Meeting>> GetMeetingsAsync(MeetingsQuery searchQuery) 
{ 
    var meetings = new List<Meeting>(); 

    //some random result manually for demo purposes 
    for (int i = 1; i <= 5; i++) 
    { 
     var random = new Random(i); 
     Meeting meeting = new Meeting() 
     { 
      DateTime = $" Available time: {searchQuery.Date} At building {i}", 
      Teacher = $" Professor {searchQuery.Name}", 
      Location = $" Elisagårdsvej 3, Room {random.Next(1, 300)}", 
      Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Supervision+{i}&w=500&h=260" 
     }; 

     meetings.Add(meeting); 
    } 

    return meetings; 
} 

最奇怪的是这段代码已经工作了,我大喊答题节目环节和尊重去为社会上GitHub,因为我认为这是一个很棒的平台,包含大量的文档和示例。

回答

4

这是已知问题(也报告herehere)。

长话短说,因为builtin.datetimeV2。*实体不BotBuilder尚不支持,该Resolution字典EntityRecommendation的与JArray类型的值的条目结束。当您将这些实体传递给FormDialog时会出现问题。由于实体是对话框中的私有字段,当然,与任何其他对话框一样,它们正在被序列化,因为Newtonsoft中的JArray类没有标记为可序列化,所以抛出了异常。

添加对datetimeV2实体的支持请求是here

我现在能想到的解决方法是手动提取的DateTime实体的价值,并为其赋予一个您MeetingsQuery实例您Date领域要传递到FormDialog,也删除从result.Entities收集日期时间实体你传递给FormDialog

更新

这已经是固定在SDK中,你可以看到this Pull Request

+1

非常感谢@ejadib的帮助,我做了什么,它解决了错误:删除了具有子代** builtin.dateTimeV2 **的MeetingDate复合实体并添加为新的自定义(类型:简单)。再次感谢您的快速响应!保持良好的精神! – ystvan