使用 Unity 而不是 ASP.Net Core DI IServiceCollection
Using Unity instead of ASP.Net Core DI IServiceCollection
越来越多的 .NET Core 库绑定到 IServiceCollection
。例如,我想在我的 NET Framework 4.7.1 中使用 HttpClientFactory
描述的 here。桌面应用程序。我的应用程序正在使用 Unity IoC。我将 Microsoft.Extensions.Http
引用为 NuGet。
但是有一个问题:新的ASP.Net 核心组件绑定到.NetCore 的Microsoft DI 框架- IServiceCollection
。例如,HttpClientFactory
的注册在这里:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
我正在深入MS code,想手动注册相应的接口和类到Unity。这是 IServiceCollection 注册服务的方式:
services.TryAddTransient<HttpMessageHandlerBuilder, DefaultHttpMessageHandlerBuilder>();
services.TryAddSingleton<IHttpClientFactory, DefaultHttpClientFactory>();
将它移动到 Unity IoC 是没有问题的,但是当我想注册 DefaultHttpMessageHandlerBuilder
和 DefaultHttpClientFactory
时我卡住了,它们有 internal能见度。所以它们不能在 MS 代码之外注册。
我有机会解决这个问题吗?
您有 2 个选择:
创建一个 ServiceCollection,添加工厂,然后调用 BuildServiceProvider 并解析 IHttpClientFactory。这里有一个超级样本 https://github.com/aspnet/HttpClientFactory/blob/64ed5889635b07b61923ed5fd9c8b69c997deac0/samples/HttpClientFactorySample/Program.cs#L21.
使用统一适配器 IServiceCollection
https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection/。
根据@davidfowl 的回答,我使用了他的第二个解决方案并完成了代码:
需要从我的项目中引用这些包(.csproj 中的片段):
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http">
<Version>2.1.1</Version>
</PackageReference>
<PackageReference Include="Unity.Microsoft.DependencyInjection">
<Version>2.0.10</Version>
</PackageReference>
</ItemGroup>
下面是可以从 Unity 容器解析来自 ServiceCollection 的服务的测试:
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Unity;
using Unity.Microsoft.DependencyInjection;
using Xunit;
namespace FunctionalTests
{
public class UnityWithHttpClientFactoryTest
{
/// <summary>
/// Integration of Unity container with MS ServiceCollection test
/// </summary>
[Fact]
public void HttpClientCanBeCreatedByUnity()
{
UnityContainer unityContainer = new UnityContainer();
ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient("Google", (c) =>
{
c.BaseAddress = new Uri("https://google.com/");
});
serviceCollection.BuildServiceProvider(unityContainer);
Assert.True(unityContainer.IsRegistered<IHttpClientFactory>());
IHttpClientFactory clientFactory = unityContainer.Resolve<IHttpClientFactory>();
HttpClient httpClient = clientFactory.CreateClient("Google");
Assert.NotNull(httpClient);
Assert.Equal("https://google.com/", httpClient.BaseAddress.ToString());
}
}
}
越来越多的 .NET Core 库绑定到 IServiceCollection
。例如,我想在我的 NET Framework 4.7.1 中使用 HttpClientFactory
描述的 here。桌面应用程序。我的应用程序正在使用 Unity IoC。我将 Microsoft.Extensions.Http
引用为 NuGet。
但是有一个问题:新的ASP.Net 核心组件绑定到.NetCore 的Microsoft DI 框架- IServiceCollection
。例如,HttpClientFactory
的注册在这里:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
我正在深入MS code,想手动注册相应的接口和类到Unity。这是 IServiceCollection 注册服务的方式:
services.TryAddTransient<HttpMessageHandlerBuilder, DefaultHttpMessageHandlerBuilder>();
services.TryAddSingleton<IHttpClientFactory, DefaultHttpClientFactory>();
将它移动到 Unity IoC 是没有问题的,但是当我想注册 DefaultHttpMessageHandlerBuilder
和 DefaultHttpClientFactory
时我卡住了,它们有 internal能见度。所以它们不能在 MS 代码之外注册。
我有机会解决这个问题吗?
您有 2 个选择:
创建一个 ServiceCollection,添加工厂,然后调用 BuildServiceProvider 并解析 IHttpClientFactory。这里有一个超级样本 https://github.com/aspnet/HttpClientFactory/blob/64ed5889635b07b61923ed5fd9c8b69c997deac0/samples/HttpClientFactorySample/Program.cs#L21.
使用统一适配器
IServiceCollection
https://www.nuget.org/packages/Unity.Microsoft.DependencyInjection/。
根据@davidfowl 的回答,我使用了他的第二个解决方案并完成了代码:
需要从我的项目中引用这些包(.csproj 中的片段):
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http">
<Version>2.1.1</Version>
</PackageReference>
<PackageReference Include="Unity.Microsoft.DependencyInjection">
<Version>2.0.10</Version>
</PackageReference>
</ItemGroup>
下面是可以从 Unity 容器解析来自 ServiceCollection 的服务的测试:
using System;
using System.Linq;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Unity;
using Unity.Microsoft.DependencyInjection;
using Xunit;
namespace FunctionalTests
{
public class UnityWithHttpClientFactoryTest
{
/// <summary>
/// Integration of Unity container with MS ServiceCollection test
/// </summary>
[Fact]
public void HttpClientCanBeCreatedByUnity()
{
UnityContainer unityContainer = new UnityContainer();
ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient("Google", (c) =>
{
c.BaseAddress = new Uri("https://google.com/");
});
serviceCollection.BuildServiceProvider(unityContainer);
Assert.True(unityContainer.IsRegistered<IHttpClientFactory>());
IHttpClientFactory clientFactory = unityContainer.Resolve<IHttpClientFactory>();
HttpClient httpClient = clientFactory.CreateClient("Google");
Assert.NotNull(httpClient);
Assert.Equal("https://google.com/", httpClient.BaseAddress.ToString());
}
}
}