如何解决在 UnityContainer 中注册 IHttpClientFactory
我正在尝试注册 IHttpClientFactory
。首先,我已将其实施到我的服务中:
public class WebApiService : IWebApiService
{
private readonly IHttpClientFactory _httpClientFactory;
public WebApiService(IHttpClientFactory httpClientFactory) => _httpClientFactory = httpClientFactory;
}
尽管如此,我不确定如何在 DI UnityContainer
中正确注册它。谁能帮帮我?
namespace MobileAppXamarinForms
{
public partial class App : Application
{
var container = new UnityContainer()
.AddExtension(new Diagnostic())
.RegisterInstance(mapper)
.RegisterType<IDb,Db>(new ContainerControlledLifetimeManager())
.RegisterType<IPageService,PageService>(new ContainerControlledLifetimeManager())
}
...
}
到目前为止,我已经尝试添加 ServiceCollection
并注册它,我希望我能帮助解决 IHttpClientFactory
,但它不起作用,我收到如下错误:
namespace MobileAppXamarinForms
{
public partial class App : Application
{
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
serviceCollection.BuildServiceProvider();
var container = new UnityContainer()
.AddExtension(new Diagnostic())
.RegisterInstance(mapper)
.RegisterInstance(serviceCollection) // <======
.RegisterType<IDb,Db>(new ContainerControlledLifetimeManager())
.RegisterType<IPageService,PageService>(new ContainerControlledLifetimeManager())
}
...
}
当调试错误显示时:
Unity.ResolutionFailedException: 'The current type,System.Net.Http.IHttpClientFactory,is an interface and cannot be constructed. Are you missing a type mapping?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。