为什么不能让AutoFac解析我的课程?

时间:2018-02-25 10:33:00

标签: c# autofac

我遇到问题,AutoFac能够解析构造函数来解析我的类型

我有一个基本类型DataProviderBase

public abstract class DataProviderBase<TEntity>
  where TEntity : class, IEntity
{
  protected DataProviderBase()
  {
  }
}

这有一个相关的界面

public interface IDataProvider<TEntity>
    where TEntity : class, IEntity
{

}

然后我有继承自此

的BidDataProvider
public class BidDataProvider : DataProviderBase<Bid>, IBidDataProvider
{
    public BidDataProvider()
    {

    }
}

这有一个相关的界面

public interface IBidDataProvider : IDataProvider<Bid>
{

}

在我的模块中,我注册了数据提供者

public class LivePriceManagerModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<BidDataProvider>()
            .As<IBidDataProvider>()
            .SingleInstance();
    }
}

然后我有一个NServiceBus消息处理程序,我需要将BidDataProvider注入

public class CreateBidHandler : IHandleMessages<CreateBid>
{
    private IBidDataProvider _bidDataProvider;

    public CreateBidHandler(IBidDataProvider bidDataProvider)
    {
        Argument.IsNotNull(() => bidDataProvider);

        _bidDataProvider = bidDataProvider;
    }
}

当我运行我的应用程序时,我收到以下错误

Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = CreateBidHandler (ReflectionActivator), Services = [NServiceBus.IHandleMessages`1[[Inspired.TradingPlatform.Messages.Commands.CreateBid, Inspired.TradingPlatform.Messages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Inspired.TradingPlatform.LivePriceManager.Handlers.CreateBidHandler], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Inspired.TradingPlatform.LivePriceManager.Handlers.CreateBidHandler' can be invoked with the available services and parameters:
Cannot resolve parameter 'Inspired.TradingPlatform.LivePriceManager.Services.IBidDataProvider bidDataProvider' of constructor 'Void .ctor(Inspired.TradingPlatform.LivePriceManager.Services.IBidDataProvider)'. (See inner exception for details.) ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Inspired.TradingPlatform.LivePriceManager.Handlers.CreateBidHandler' can be invoked with the available services and parameters:
Cannot resolve parameter 'Inspired.TradingPlatform.LivePriceManager.Services.IBidDataProvider bidDataProvider' of constructor 'Void .ctor(Inspired.TradingPlatform.LivePriceManager.Services.IBidDataProvider)'.
   at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)

有谁知道为什么我的构造函数无法找到?

Autofac应该能够看到它,因为BidDataProvider没有需要参数的构造函数

我在类型注册和处理程序

中尝试了具体类和接口的各种组合

1 个答案:

答案 0 :(得分:0)

解决方案是确保在应用程序启动时加载所有程序集。没有这个,类型没有正确注册