我試圖(徒勞)註冊我的對話框。 我對話的構造是這樣的:Autofac服務未註冊(Microsoft Bot框架)
// 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);
}
在我PiiiCKModule,我已經做到了這一點:
public class PiiiCKModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
// Register our Luis Attribute
builder.Register(c => new LuisModelAttribute("key", "key")).AsSelf().AsImplementedInterfaces().SingleInstance();
// Register some singleton services
builder.RegisterType<GroupProvider>().Keyed<IGroupProvider>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();
builder.RegisterType<ProductProvider>().Keyed<IProductProvider>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();
// Register the top level dialog
builder.RegisterType<PiiiCKDialog>().As<LuisDialog<object>>().InstancePerDependency();
}
}
在我Global.ascx.cs文件我按照the Autofac quick start創造這個:
public class WebApiApplication : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
// Create our builder
var builder = new ContainerBuilder();
var config = GlobalConfiguration.Configuration;
// Register the alarm dependencies
builder.RegisterModule(new PiiiCKModule());
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// OPTIONAL: Register the Autofac filter provider.
builder.RegisterWebApiFilterProvider(config);
// Build.
var container = builder.Build();
// Set the dependency resolver to be Autofac.
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
// Configure our Web API
GlobalConfiguration.Configure(WebApiConfig.Register);
}
public static ILifetimeScope FindContainer()
{
var config = GlobalConfiguration.Configuration;
var resolver = (AutofacWebApiDependencyResolver)config.DependencyResolver;
return resolver.Container;
}
}
我的控制器如下所示:
[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);
}
}
但是當我運行我的應用程序,我得到這個錯誤:
'PiiiCKBot.Business.PiiiCKDialog' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
至於我可以告訴大家,我是註冊我的組件。有沒有人有任何線索爲什麼這不起作用?