【问题标题】:Unity The type does not have an accessible constructorunity 类型没有可访问的构造函数
【发布时间】:2014-02-27 10:46:59
【问题描述】:

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

这里是注册类型的代码:

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

【问题讨论】:

  • 为什么需要为IConsentService提供注入工厂。所有必需的接口都已注册,因此默认注册应该是好的,Unity 会自动解决它们。你检查了 Consent 服务构造函数是否是公共的?
  • 我现在尝试更改问题中显示的代码。它也没有那样工作。 Consent Service 类是公共的,所涉及的其他类也是公共的。
  • 现在改注册了,还是一样的错误吗?另外,您能否发布您的 IConsentService 和 ConsentService 类
  • 是的,同样的错误。我没有在类中包含代码,因为该类有 240 行,我想这没关系。但我注意到,在写这篇文章时,我的第一步是创建 ITimer,但忘记更改解析 ConsentStatusPoller 的位置以改用 ITimer。所以现在错误是一样的,但它说 ITimer 没有可访问的构造函数。我所说的那一行是“var consentPoller = IoC.Resolve();”

标签: c# unity-container


【解决方案1】:

此错误看起来像容器不知道 IConsentService 已映射到 ConsentService。您的所有注册看起来都是正确的,所以我唯一的猜测是在您致电 Resolve 之前确保您的所有注册都已完成。要么,要么你有一个电话Container.RegisterType&lt;IConsentService&gt;(),它覆盖了你对Container.RegisterType&lt;IConsentService,ConsentService&gt;()的电话

【讨论】:

    【解决方案2】:

    我刚刚遇到同样的错误。就我而言,它更明显(也更尴尬)。我试图解析的类的构造函数受到保护,我从另一个程序集中解析它。

    所以报错信息其实很准确……

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

    我确定您现在已经解决了,但请检查构造函数是否应该公开。

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-05
        • 1970-01-01
        相关资源
        最近更新 更多