如何更改默认“抱歉,我的机器人代码有问题”异常消息?

时间:2017-03-23 11:44:34

标签: c# botframework

我需要更改默认设置“抱歉,我的机器人代码有问题”异常消息。这似乎是一个有点复杂的过程。我试图像在博客文章中所说的那样做:http://wp.sjkp.dk/change-the-sorry-my-bot-code-is-having-an-issue-in-microsoft-bot-framework/

我不了解依赖注入或控制反转的知识,因此这变得非常具有挑战性。我正在使用Bot Builder版本3.5.5。

这是博客中的代码,我试图在我的机器人上使用:

PostUnhandledExceptionToUser类:

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Connector;
using System;
using System.Diagnostics;
using System.Net.Mime;
using System.Resources;
using System.Threading;
using System.Threading.Tasks;

namespace Tiimo.Bot.BotFramework
{
    public class PostUnhandledExceptionToUser : IPostToBot
    {
        private readonly ResourceManager resources;
        private readonly IPostToBot inner;
        private readonly IBotToUser botToUser;
        private readonly TraceListener trace;

        public PostUnhandledExceptionToUser(IPostToBot inner, IBotToUser botToUser, ResourceManager resources, TraceListener trace)
        {
            SetField.NotNull(out this.inner, nameof(inner), inner);
            SetField.NotNull(out this.botToUser, nameof(botToUser), botToUser);
            SetField.NotNull(out this.resources, nameof(resources), resources);
            SetField.NotNull(out this.trace, nameof(trace), trace);
        }

        async Task IPostToBot.PostAsync(IActivity activity, CancellationToken token)
        {
            try
            {
                await this.inner.PostAsync(activity, token);
            }
            catch (Exception error)
            {
                try
                {
                    if (Debugger.IsAttached)
                    {
                        var message = this.botToUser.MakeMessage();
                        message.Text = $"Exception: { error.Message}";
                        message.Attachments = new[]
                        {
                            new Attachment(contentType: MediaTypeNames.Text.Plain, content: error.StackTrace)
                        };

                        await this.botToUser.PostAsync(message);
                    }
                    else
                    {
                        await this.botToUser.PostAsync("My Personal Error Message");
                    }
                }
                catch (Exception inner)
                {
                    this.trace.WriteLine(inner);
                }

                throw;
            }
        }
    }
}

DefaultExceptionMessageOverrideModule类:

using Autofac;
using Autofac.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Dialogs.Internals;
using Microsoft.Bot.Builder.History;
using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Scorables.Internals;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Resources;
using System.Web;


namespace Tiimo.Bot.BotFramework
{
    public class DefaultExceptionMessageOverrideModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<PostUnhandledExceptionToUser>().Keyed<IPostToBot>(typeof(PostUnhandledExceptionToUser)).InstancePerLifetimeScope();


            RegisterAdapterChain<IPostToBot>(builder, 
                typeof(PersistentDialogTask),
                typeof(ExceptionTranslationDialogTask),
                typeof(SerializeByConversation),
                typeof(SetAmbientThreadCulture),
                typeof(PostUnhandledExceptionToUser),
                typeof(LogPostToBot)
            )
            .InstancePerLifetimeScope();
        }

        public static IRegistrationBuilder<TLimit, SimpleActivatorData, SingleRegistrationStyle> RegisterAdapterChain<TLimit>(ContainerBuilder builder, params Type[] types)
        {
            return
                builder
                .Register(c =>
                {
                // http://stackoverflow.com/questions/23406641/how-to-mix-decorators-in-autofac
                TLimit service = default(TLimit);
                    for (int index = 0; index < types.Length; ++index)
                    {
                    // resolve the keyed adapter, passing the previous service as the inner parameter
                    service = c.ResolveKeyed<TLimit>(types[index], TypedParameter.From(service));
                    }

                    return service;
                })
                .As<TLimit>();
        }
    }  
}

创建这些类之后,我转到了Global.asax文件并输入了以下代码:

var builder = new ContainerBuilder();
builder.RegisterModule(new DefaultExceptionMessageOverrideModule());
builder.Update(Conversation.Container);

它构建成功,但是当我向机器人发送消息时,我收到以下错误:

  

在调用构造函数'Void .ctor时抛出异常(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,   类型上的Microsoft.Bot.Builder.Dialogs.Internals.IBotData)'   “PersistentDialogTask”。 - &GT; Valornãopodeser nulo。 Nomedoparâmetro:   内部(详见内部异常。)

我认为它转化为类似

  

在调用构造函数'Void时抛出异常   .ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,   类型上的Microsoft.Bot.Builder.Dialogs.Internals.IBotData)'   “PersistentDialogTask”。 - &GT;值不能为空。参数名称:inner   (详见内部异常。)

代码有问题吗?我究竟做错了什么?还有另一种方法吗?

1 个答案:

答案 0 :(得分:3)

尝试使用此代码注册适配器链:

builder
        .RegisterAdapterChain<IPostToBot>
        (
            typeof(EventLoopDialogTask),
            typeof(SetAmbientThreadCulture),
            typeof(PersistentDialogTask),
            typeof(ExceptionTranslationDialogTask),
            typeof(SerializeByConversation),
            typeof(PostUnhandledExceptionToUser),
            typeof(LogPostToBot)
        )
        .InstancePerLifetimeScope();

我相信在某些时候PersistentDialogTask已更新,they added a dependency on an IPostToBot implementation在您使用的注册中未提供。DialogModule。我从{{3}}获取了更新的代码。

如果您查看代码,您会看到EventLoopDialogTask是链中的第一项,并且它不依赖于任何IPostToBot实施。我的猜测是,在您阅读的帖子中,PersisteDialogTask是该链中的第一个项目。