简体   繁体   English

Autofac服务未注册(Microsoft Bot Framework)

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

I am trying (in vain) to register my Dialog. 我正在努力(徒劳)注册我的Dialog。 My Dialog's constructor look like this: 我的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);
}

In my PiiiCKModule , I have done this: 在我的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();
    }
}

And in my Global.ascx.cs file I have followed the Autofac quick start and created this: 在我的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;
    }
}

My controller looks like this: 我的控制器看起来像这样:

[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);
    }
}

But when I run my application, I get this error: 但是当我运行我的应用程序时,我收到此错误:

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

As far as I can tell, I am registering my component. 据我所知,我正在注册我的组件。 Does anyone have any clue why this is not working? 有没有人知道为什么这不起作用?

Ok, I managed to get this to work: 好的,我设法让这个工作:

First, in my Message controller I changed it to this: 首先,在我的Message控制器中,我将其更改为:

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

It also appears that I have to add the [NonSerialized] attribute to the providers, which I was certain I wounldn't have to because of the Module where I do this: 我似乎还必须将[NonSerialized]属性添加到提供者,我确信我没有必要,因为我执行此操作的模块

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

But it won't work without the data attribute. 但如果没有data属性,它将无法工作。 Finally, in my Module when I register the Dialog, it should be registered like this: 最后,在我注册Dialog的模块中 ,它应该像这样注册:

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

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

相关问题 使用Autofac在Microsoft Bot Framework对话框中注入外部依赖项 - Injecting external dependencies in Microsoft Bot Framework Dialog using Autofac 如何在Microsoft Bot框架中获取已部署的机器人应用程序的服务URL? - How to get the Service URL of a deployed bot application in Microsoft Bot framework? 测试失败:没有注册“Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]”类型的服务 - Tests fail: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Autofac.ContainerBuilder]' has been registered 没有注册“Microsoft.Framework.Runtime.ILibraryManager”类型的服务 - No service for type 'Microsoft.Framework.Runtime.ILibraryManager' has been registered Autofac - 请求的服务 (lSomeDbContext) 尚未注册 - Autofac - The requested service (lSomeDbContext) has not been registered Autofac无法解析我的注册通用服务 - Autofac cannot resolve my Registered generic service 如何在Autofac中获取所有注册的服务类型 - How to get all registered service types in Autofac 已注册服务中的Autofac属性注入 - Autofac Property Injection in already registered service Microsoft Bot框架和附件 - Microsoft Bot Framework and attachments 带有LUIS的Microsoft Bot Framework - Microsoft Bot Framework with LUIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM