gpt4 book ai didi

c# - Unity 类型没有可访问的构造函数

转载 作者:行者123 更新时间:2023-11-30 18:58:43 26 4
gpt4 key购买 nike

我最近开始使用 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>())));

异常(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)

我在这里做错了什么?一开始我以为我有一些私有(private)的东西,但事实并非如此

将注册更改为:

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 映射到 ConsentService。您所有的注册看起来都是正确的,所以我唯一的猜测是在调用 Resolve 之前确保您的所有注册都已完成。要么,要么你打个电话Container.RegisterType<IConsentService>()那将覆盖您对 Container.RegisterType<IConsentService,ConsentService>() 的调用

关于c# - Unity 类型没有可访问的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22066173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com