c# – 如何更改默认值“抱歉,我的机器人代码有问题”异常消息?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何更改默认值“抱歉,我的机器人代码有问题”异常消息?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4930字,纯文字阅读大概需要8分钟。
内容图文
我需要更改默认的“抱歉,我的机器人代码有问题”异常消息.这似乎是一个有点复杂的过程.我试图像在博客文章中所说的那样做: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 =>
{
// https://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);
它成功构建,但是当我向bot发送消息时,我收到以下错误:
An exception was thrown while invoking the constructor ‘Void .ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,
Microsoft.Bot.Builder.Dialogs.Internals.IBotData)’ on type
‘PersistentDialogTask’. —> Valor não pode ser nulo. Nome do parâmetro:
inner (See inner exception for details.)
我想这可以转化为类似的东西
An exception was thrown while invoking the constructor ‘Void
.ctor(Microsoft.Bot.Builder.Dialogs.Internals.IPostToBot,
Microsoft.Bot.Builder.Dialogs.Internals.IBotData)’ on type
‘PersistentDialogTask’. —> Value cannot be null. Parameter name: inner
(See inner exception for details.)
代码有问题吗?我究竟做错了什么?还有另一种方法吗?
解决方法:
尝试使用此代码注册适配器链:
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获取了更新的代码.
如果您查看代码,您将看到EventLoopDialogTask是链中的第一个项目,它不依赖于任何IPostToBot实现.我的猜测是,在您阅读的帖子时,PersisteDialogTask是该链中的第一个项目.
内容总结
以上是互联网集市为您收集整理的c# – 如何更改默认值“抱歉,我的机器人代码有问题”异常消息?全部内容,希望文章能够帮你解决c# – 如何更改默认值“抱歉,我的机器人代码有问题”异常消息?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。