Недавно я начал использовать единство и, кажется, хорошо работает, за исключением одного места ... Я сделал некоторые поиски, но я не нашел ничего, что мог бы применить к моему коду.Unity Тип не имеет доступного конструктора
Heres код для зарегистрировать типы:
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. Все необходимые интерфейсы уже зарегистрированы, поэтому регистрация defaul должна быть хорошей, и Unity автоматически разрешит их. Вы проверили, открыт ли конструктор службы согласия? –
Я попытался изменить код, как показано в вопросе сейчас. Так не получилось. Класс службы согласия является общедоступным, а также задействованы другие классы. – user1842278
Является ли ошибка той же, теперь, когда вы изменили регистрацию? Также вы можете опубликовать свои классы IConsentService и ConsentService – mmilleruva