我最近开始使用团结和除了一个地方似乎运作良好......我做了一些搜索,但我没有找到任何可以应用于我的代码。统一该类型没有一个可访问的构造函数
继承人的代码注册类型:
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会自动解决它们。 您是否检查同意服务构造函数是否公开? –
我试图改变现在问题中显示的代码。它也没有那样工作。同意服务类是公开的,所涉及的其他类也是公共的。 – user1842278
错误是否一样,现在您更改了注册?另外,你可以发布你的IConsentService和ConsentService类 – mmilleruva