繁体   English   中英

Autofac服务未注册(Microsoft Bot Framework)

[英]Autofac service not registered (Microsoft Bot Framework)

我正在努力(徒劳)注册我的Dialog。 我的Dialog的构造函数如下所示:

// 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文件中,我遵循Autofac快速入门并创建了这个:

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'尚未注册。 要避免此异常,请注册组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解析可选依赖项。

据我所知,我正在注册我的组件。 有没有人知道为什么这不起作用?

好的,我设法让这个工作:

首先,在我的Message控制器中,我将其更改为:

await Conversation.SendAsync(model, () => scope.Resolve<IDialog<object>>());

我似乎还必须将[NonSerialized]属性添加到提供者,我确信我没有必要,因为我执行此操作的模块

builder.RegisterType<GroupProvider>().Keyed<IGroupProvider>(FiberModule.Key_DoNotSerialize).AsImplementedInterfaces().SingleInstance();

但如果没有data属性,它将无法工作。 最后,在我注册Dialog的模块中 ,它应该像这样注册:

builder.RegisterType<PiiiCKDialog>().As<IDialog<object>>().InstancePerDependency();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM