Unity类型没有可访问的构造函数

时间:2014-02-27 10:47:00

标签: c# unity-container

我最近开始使用统一,它似乎运行良好,除了一个地方......我做了一些搜索,但我找不到任何我可以应用于我的代码。

下面是注册类型的代码:

Container.RegisterType<IVsoCommunicator, VsoCommunicator>();
Container.RegisterType<IIpxConnection, IpxCommunicator>();
Container.RegisterType<IConsentService, ConsentService>(new InjectionFactory(p => new ConsentService(p.Resolve<IIpxConnection>(), p.Resolve<IVsoCommunicator>())));
Container.RegisterType<ITimer, ConsentStatusPoller>(new InjectionFactory(p => new ConsentStatusPoller(p.Resolve<IConsentService>())));

例外:

Resolution of the dependency failed, type = "UserManager.Services.ConsentStatusPoller", name = "(none)".
Exception occurred while: while resolving.

Exception is: InvalidOperationException - The type IConsentService does not have an accessible constructor.

-----------------------------------------------

At the time of the exception, the container was:

Resolving UserManager.Services.ConsentStatusPoller,(none)

Resolving parameter "consentService" of constructor UserManager.Services.ConsentStatusPoller(UserManager.Services.IConsentService consentService)

Resolving UserManager.Services.IConsentService,(none)

我在这里做错了什么?首先,我认为我有私事但事实并非如此

将注册更改为:

Container.RegisterType<ISettingsConfiguration, SettingsConfiguration>();
Container.RegisterType<IUnitOfWork, UnitOfWork>();
Container.RegisterType<IVsoCommunicator, VsoCommunicator>();
Container.RegisterType<IIpxConnection, IpxCommunicator>();
Container.RegisterType<IConsentService, ConsentService>();
Container.RegisterType<ITimer, ConsentStatusPoller>();

同意服务:

public interface IConsentService
{
    void GetConsentReplies();
    void GetConsentSmsUpdates();
}
public class ConsentService : IConsentService
{
    private readonly IIpxConnection _ipx;
    private readonly IVsoCommunicator _vso;

    public ConsentService(IIpxConnection ipx, IVsoCommunicator vso)
    {
        _ipx = ipx;
        _vso = vso;
    }

    public async void GetConsentReplies()
    {
    }

    private void UpdateLocationConsentSmsData(List<IncomingMessage> messages)
    {

    }

    private void SendConsentMessages(IEnumerable<IncomingMessage> messages)
    {


    }

    private void SaveIpxConsents(IEnumerable<createConsentResponse1> responses)
    {
    }

    public async void GetConsentSmsUpdates()
    {

    }

    private static void SaveConsentSmsUpdates(GetMessageStatusResponse resp)
    {

    }

}

同意民意调查者:

public interface ITimer
{
    void Start(int interval);
}
public class ConsentStatusPoller : ITimer
{
    private readonly IConsentService _consentService;
    readonly Timer _tmr;

    public ConsentStatusPoller(IConsentService consentService)
    {
        _consentService = consentService;
        _tmr = new Timer();
        _tmr.Elapsed += _tmr_Elapsed;
    }

    void _tmr_Elapsed(object sender, ElapsedEventArgs e)
    {
        _consentService.GetConsentReplies();
        _consentService.GetConsentSmsUpdates();
    }



    public void Start(int interval)
    {
        _tmr.Interval = interval;
        _tmr.Start();
    }
}

3 个答案:

答案 0 :(得分:8)

此错误看起来容器不知道IConsentService已映射到ConsentService。您的所有注册看起来都是正确的,所以我唯一的猜测就是确保在您致电Resolve之前完成所有注册。或者,或者您有一个电话Container.RegisterType<IConsentService>()覆盖了您对Container.RegisterType<IConsentService,ConsentService>()的呼叫

答案 1 :(得分:5)

我得到了同样的错误。在我的情况下,它更明显(并且更加尴尬)。我尝试解决的类的构造函数受到保护,我从另一个程序集中解析它。

因此错误信息实际上非常准确......

  

该类型没有可访问的构造函数

我确定你现在已经解决了这个问题,但检查一下构造函数是否公开。

答案 2 :(得分:0)

对我来说,这是由于软件包不匹配造成的。

我的Web项目引用了一个nuget包,我的服务项目引用了一个不同的版本。

我是通过IAmazonS3来获得此功能的,它以前是AWSSDK的一部分,但现在在AWSSDK.S3中。一个项目引用了旧项目。

相关问题