2014-02-27 17 views
2

我最近开始使用团结和除了一个地方似乎运作良好......我做了一些搜索,但我没有找到任何可以应用于我的代码。统一该类型没有一个可访问的构造函数

继承人的代码注册类型:

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

为什么您需要为IConsentService提供注塑工厂。所有必需的接口已经被注册,因此默认注册应该很好,Unity会自动解决它们。 您是否检查同意服务构造函数是否公开? –

+0

我试图改变现在问题中显示的代码。它也没有那样工作。同意服务类是公开的,所涉及的其他类也是公共的。 – user1842278

+0

错误是否一样,现在您更改了注册?另外,你可以发布你的IConsentService和ConsentService类 – mmilleruva

回答

7

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

3

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

所以错误消息其实很准确......

的类型不具有可访问的构造

我敢肯定,你现在解决了这个问题,但检查施工人员应该是公开的。

相关问题