【发布时间】: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