C# 未注册Autofac服务(Microsoft Bot Framework)
我试图(徒劳地)注册我的对话。 我的对话框的构造函数如下所示:C# 未注册Autofac服务(Microsoft Bot Framework),c#,autofac,botframework,C#,Autofac,Botframework,我试图(徒劳地)注册我的对话。 我的对话框的构造函数如下所示: // Private fields protected readonly IGroupProvider _groupProvider; protected readonly IProductProvider _productProvider; protected IList<GroupResponseModel> _groups; protected IList<ProductResponseModel>
// Private fields
protected readonly IGroupProvider _groupProvider;
protected readonly IProductProvider _productProvider;
protected IList<GroupResponseModel> _groups;
protected IList<ProductResponseModel> _products;
/// <summary>
/// Default constructor
/// </summary>
public PiiiCKDialog(IGroupProvider groupProvider, IProductProvider productProvider)
{
SetField.NotNull(out this._groupProvider, nameof(groupProvider), groupProvider);
SetField.NotNull(out this._productProvider, nameof(productProvider), productProvider);
}
[BotAuthentication]
public class MessagesController : ApiController
{
// TODO: "service locator"
private readonly ILifetimeScope scope;
public MessagesController(ILifetimeScope scope)
{
SetField.NotNull(out this.scope, nameof(scope), scope);
}
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity model, CancellationToken token)
{
// one of these will have an interface and process it
switch (model.GetActivityType())
{
case ActivityTypes.Message:
try
{
// Create our conversation
await Conversation.SendAsync(model, () => scope.Resolve<PiiiCKDialog>());
}
catch (Exception ex)
{
}
break;
case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
default:
Trace.TraceError($"Unknown activity type ignored: { model.GetActivityType() }");
break;
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
}
builder.RegisterType<PiiiCKDialog>().As<IDialog<object>>().InstancePerDependency();
我的控制器如下所示:
// Private fields
protected readonly IGroupProvider _groupProvider;
protected readonly IProductProvider _productProvider;
protected IList<GroupResponseModel> _groups;
protected IList<ProductResponseModel> _products;
/// <summary>
/// Default constructor
/// </summary>
public PiiiCKDialog(IGroupProvider groupProvider, IProductProvider productProvider)
{
SetField.NotNull(out this._groupProvider, nameof(groupProvider), groupProvider);
SetField.NotNull(out this._productProvider, nameof(productProvider), productProvider);
}
[BotAuthentication]
public class MessagesController : ApiController
{
// TODO: "service locator"
private readonly ILifetimeScope scope;
public MessagesController(ILifetimeScope scope)
{
SetField.NotNull(out this.scope, nameof(scope), scope);
}
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity model, CancellationToken token)
{
// one of these will have an interface and process it
switch (model.GetActivityType())
{
case ActivityTypes.Message:
try
{
// Create our conversation
await Conversation.SendAsync(model, () => scope.Resolve<PiiiCKDialog>());
}
catch (Exception ex)
{
}
break;
case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
default:
Trace.TraceError($"Unknown activity type ignored: { model.GetActivityType() }");
break;
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
}
builder.RegisterType<PiiiCKDialog>().As<IDialog<object>>().InstancePerDependency();
[BotAuthentication]
公共类消息控制器:ApiController
{
//TODO:“服务定位器”
私有只读ILifetimeScope作用域;
公共消息控制器(ILifetimeScope作用域)
{
SetField.NotNull(不包括this.scope、nameof(scope)、scope);
}
///
///帖子:api/Messages
///接收来自用户的消息并回复
///
公共异步任务发布([FromBody]活动模型,CancellationToken)
{
//其中一个将有一个接口并处理它
开关(model.GetActivityType())
{
案例活动类型。消息:
尝试
{
//创造我们的对话
wait Conversation.sendaync(model,()=>scope.Resolve());
}
捕获(例外情况除外)
{
}
打破
案例活动类型.ConversationUpdate:
案例活动类型。联系人关系更新:
案例活动类型。键入:
案例ActivityTypes.DeleteUserData:
违约:
Trace.TraceError($“忽略未知活动类型:{model.GetActivityType()}”);
打破
}
返回新的HttpResponseMessage(HttpStatusCode.Accepted);
}
}
但是,当我运行应用程序时,会出现以下错误:
尚未注册“PiiiCKBot.Business.PiiiCKDialog”。要避免此异常,请注册组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解析可选依赖项
据我所知,我正在注册我的组件。有人知道为什么这不起作用吗?好的,我设法让它起作用了: 首先,在我的消息控制器中,我将其更改为:
await Conversation.SendAsync(model, () => scope.Resolve<IDialog<object>>());
wait Conversation.sendaync(model,()=>scope.Resolve());
似乎我必须向提供者添加[NonSerialized]属性,我确信我不必这样做,因为我在模块中这样做:
builder.RegisterType<GroupProvider>().Keyed<IGroupProvider>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();
builder.RegisterType();
但如果没有数据属性,它将无法工作。
最后,在我的模块中注册对话框时,应按如下方式注册:
// Private fields
protected readonly IGroupProvider _groupProvider;
protected readonly IProductProvider _productProvider;
protected IList<GroupResponseModel> _groups;
protected IList<ProductResponseModel> _products;
/// <summary>
/// Default constructor
/// </summary>
public PiiiCKDialog(IGroupProvider groupProvider, IProductProvider productProvider)
{
SetField.NotNull(out this._groupProvider, nameof(groupProvider), groupProvider);
SetField.NotNull(out this._productProvider, nameof(productProvider), productProvider);
}
[BotAuthentication]
public class MessagesController : ApiController
{
// TODO: "service locator"
private readonly ILifetimeScope scope;
public MessagesController(ILifetimeScope scope)
{
SetField.NotNull(out this.scope, nameof(scope), scope);
}
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity model, CancellationToken token)
{
// one of these will have an interface and process it
switch (model.GetActivityType())
{
case ActivityTypes.Message:
try
{
// Create our conversation
await Conversation.SendAsync(model, () => scope.Resolve<PiiiCKDialog>());
}
catch (Exception ex)
{
}
break;
case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
default:
Trace.TraceError($"Unknown activity type ignored: { model.GetActivityType() }");
break;
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
}
builder.RegisterType<PiiiCKDialog>().As<IDialog<object>>().InstancePerDependency();
builder.RegisterType().As().InstancePerDependence();
好的,我设法让它工作了:
首先,在我的消息控制器中,我将其更改为:
await Conversation.SendAsync(model, () => scope.Resolve<IDialog<object>>());
wait Conversation.sendaync(model,()=>scope.Resolve());
似乎我必须向提供者添加[NonSerialized]属性,我确信我不必这样做,因为我在模块中这样做:
builder.RegisterType<GroupProvider>().Keyed<IGroupProvider>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();
builder.RegisterType();
但如果没有数据属性,它将无法工作。
最后,在我的模块中注册对话框时,应按如下方式注册:
// Private fields
protected readonly IGroupProvider _groupProvider;
protected readonly IProductProvider _productProvider;
protected IList<GroupResponseModel> _groups;
protected IList<ProductResponseModel> _products;
/// <summary>
/// Default constructor
/// </summary>
public PiiiCKDialog(IGroupProvider groupProvider, IProductProvider productProvider)
{
SetField.NotNull(out this._groupProvider, nameof(groupProvider), groupProvider);
SetField.NotNull(out this._productProvider, nameof(productProvider), productProvider);
}
[BotAuthentication]
public class MessagesController : ApiController
{
// TODO: "service locator"
private readonly ILifetimeScope scope;
public MessagesController(ILifetimeScope scope)
{
SetField.NotNull(out this.scope, nameof(scope), scope);
}
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity model, CancellationToken token)
{
// one of these will have an interface and process it
switch (model.GetActivityType())
{
case ActivityTypes.Message:
try
{
// Create our conversation
await Conversation.SendAsync(model, () => scope.Resolve<PiiiCKDialog>());
}
catch (Exception ex)
{
}
break;
case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
default:
Trace.TraceError($"Unknown activity type ignored: { model.GetActivityType() }");
break;
}
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
}
builder.RegisterType<PiiiCKDialog>().As<IDialog<object>>().InstancePerDependency();
builder.RegisterType().As().InstancePerDependence();