context post 异步异常

context post async exception

使用机器人模拟器:

在 MessagesController 中有此代码:

 await Conversation.SendAsync(activity, () => new RootLuisDialog());

在 RootLuisDialog 中:

        [LuisIntent("")]
        [LuisIntent("None")]
        public async Task None(IDialogContext context, LuisResult result)
        {
            string message = $"Sorry, I did not understand '{result.Query}'. Type 'help' if you need assistance.";

            await context.PostAsync(message);

            context.Wait(this.MessageReceived);
        }

这是一个例外:

{"Type 'Microsoft.Bot.Connector.Activity' in Assembly 'Microsoft.Bot.Connector, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."} Data: {System.Collections.ListDictionaryInternal} HResult: -2146233076 HelpLink: null InnerException: null Message: "Type 'Microsoft.Bot.Connector.Activity' in Assembly 'Microsoft.Bot.Connector, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable." Source: "mscorlib" StackTrace: " at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)\r\n at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)\r\n at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()\r\n at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder)\r\n at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)\r\n at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)\r\n at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)\r\n at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)\r\n at Microsoft.Bot.Builder.Internals.Fibers.FormatterStore1.Microsoft.Bot.Builder.Internals.Fibers.IStore<T>.Save(T item)\r\n at Microsoft.Bot.Builder.Internals.Fibers.ErrorResilientStore1.Microsoft.Bot.Builder.Internals.Fibers.IStore.Save(T item)\r\n at Microsoft.Bot.Builder.Internals.Fibers.FactoryStore1.Microsoft.Bot.Builder.Internals.Fibers.IStore<T>.Save(T item)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.DialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d__211.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ReactiveDial ogTask. d__51.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.LocalizedDialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d__21.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.ScoringDialogTask1.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d__61.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.Co mpilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.d__61.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at Microsoft.Bot.Builder.Dialogs.Internals.PersistentDialogTask.<Microsoft-Bot-Builder-Dialogs-Internals-IPostToBot-PostAsync>d__61.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Conversation.d__4.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.Throw ForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Builder.Dialogs.Conversation.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at MessengerBot.MessagesController.d__1.MoveNext() in C:\myapp\MessagesController.cs:line 60" TargetSite: {System.Reflection.MemberInfo[] InternalGetSerializableMembers(System.RuntimeType)}

我不太确定我理解这里的异常是什么...... 有帮助吗?

编辑:我用带有参数的 rootluisdialog 的新构造函数解决了这个问题 - activity.

await Conversation.SendAsync(activity, () => new RootLuisDialog(activity)); 然后在 RootLuisDialog 中我有这个:

 Activity activity;
public RootLuisDialog(Activity activity)
        {
            this.activity = activity;

        }

Activity class 不可序列化,这是您看到的错误。 LuisDialog 中的所有 public 或受保护属性都需要是可序列化的,以便它们可以存储为响应之间的上下文。

假设您的对话框 class 是从 LuisDialog 继承的,那么这应该已经负责从意图中获取 activity - 通常您会使用上下文以及来自 LUIS 的结果,而不是直接来自 activity 的结果(就像您创建直接 IDialog 实现一样)。

从控制器传入它也不会执行您期望的操作,因为它不会包含在对话中的后续调用中。