Autofac服务未注册(Microsoft Bot Framework)

时间:2016-11-21 15:16:46

标签: c# autofac botframework

我正在努力(徒劳)注册我的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 文件中,我跟踪了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'尚未注册。要避免此异常,请注册组件以提供服务,使用IsRegistered()检查服务注册,或使用ResolveOptional()方法解析可选依赖项。

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

1 个答案:

答案 0 :(得分:0)

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

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

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

我似乎必须将[NonSerialized]属性添加到提供商,由于模块,我确信我不得不这样做我这样做的地方:

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

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

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