I recently started using unity and It seems to work well except for one place... I did some searching but I did not find anything that I can apply to my code.
Heres the code for register the types:
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>())));
And the exception:
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)
What do I do wrong here? First I thought that I had something private but that was not the case
Changed the registration to:
Container.RegisterType<ISettingsConfiguration, SettingsConfiguration>();
Container.RegisterType<IUnitOfWork, UnitOfWork>();
Container.RegisterType<IVsoCommunicator, VsoCommunicator>();
Container.RegisterType<IIpxConnection, IpxCommunicator>();
Container.RegisterType<IConsentService, ConsentService>();
Container.RegisterType<ITimer, ConsentStatusPoller>();
Consent service:
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)
{
}
}
Consent poller:
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();
}
}
This error looks like the container does not know that IConsentService is mapped to ConsentService. All of your registration looks correct, so my only guess would be to make sure all of your registration is being done, before your call to Resolve. Either that, or you have a call Container.RegisterType<IConsentService>() that is overriding your call to Container.RegisterType<IConsentService,ConsentService>()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With