C# 为什么AutoFac无法解析我的类?

C# 为什么AutoFac无法解析我的类?,c#,autofac,C#,Autofac,AutoFac在解析构造函数以解析我的类型时遇到问题 我有一个基本类型DataProviderBase public abstract class DataProviderBase<TEntity> where TEntity : class, IEntity { protected DataProviderBase() { } } 有人知道为什么找不到我的构造函数吗 Autofac应该能够看到它,因为BidDataProvider没有需要参数的构造函数 我在类型注

AutoFac在解析构造函数以解析我的类型时遇到问题

我有一个基本类型DataProviderBase

public abstract class DataProviderBase<TEntity>
  where TEntity : class, IEntity
{
  protected DataProviderBase()
  {
  }
}
有人知道为什么找不到我的构造函数吗

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


我在类型注册和处理程序中尝试了各种具体类和接口的组合。解决方法是确保在应用程序启动时加载所有程序集。如果没有这一点,类型将无法从错误消息中正确注册。您似乎正在向该构造函数注入
BidDataProvider
(具体类),而不是
IBidDataProvider
(接口)。代码显示正在使用的接口,但错误消息显示构造函数接受具体类(除非我误解了消息)。所以我的意思是你可能发布了错误的代码,在真实的代码处理程序中接受了具体的类?啊,我想我知道发生了什么,我想我更改了代码,但没有重新运行它,所以你看到另一条消息,我会再试一次。我也尝试过注册,但我得到了错误System.ArgumentException:“类型'Inspired.TradingPlatform.LivePriceManager.Services.IBidDataProvider'不可分配给服务'Inspired.TradingPlatform.LivePriceManager.Services.BidDataProvider'。”如果我尝试注册接口并在注册中使用具体类,您可以需要像现在一样注册,但在构造函数中接受
IBidDataProvider
。因此,基本上,您发布的代码应该可以正常工作。如果没有-请发布正确的错误消息。由于某种原因,它看不到您的注册(从
IBidManager
BidManager
)。也许你有多个容器?如何注册
CreateBidHandler
本身?
public interface IDataProvider<TEntity>
    where TEntity : class, IEntity
{

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