c# - 使用 Unity 代替 ASP.Net Core DI IServiceCollection

标签 c# asp.net-core dependency-injection unity-container

越来越多的 .NET Core 库绑定(bind)到 IServiceCollection。例如,我想使用 HttpClientFactory 描述 here在我的 NET Framework 4.7.1 中。桌面应用程序。我的应用程序正在使用 Unity IoC。我将 Microsoft.Extensions.Http 引用为 NuGet。

但是有一个问题:新的 ASP.Net Core 组件绑定(bind)到 Microsoft .NetCore 的 DI 框架 - IServiceCollection。例如,HttpClientFactory 的注册在这里:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpClient();
}

我正在深入 MS code并想手动向Unity注册相应的接口(interface)和类。这是 IServiceCollection 注册服务的方式:

services.TryAddTransient<HttpMessageHandlerBuilder, DefaultHttpMessageHandlerBuilder>();
services.TryAddSingleton<IHttpClientFactory, DefaultHttpClientFactory>();

将它移动到 Unity IoC 是没有问题的,但是当我想注册 DefaultHttpMessageHandlerBuilderDefaultHttpClientFactory 时,我陷入了困境,它们具有内部 知名度。因此,它们不能在 MS 代码之外进行注册。

我有机会解决这种情况吗?

最佳答案

编辑 7.12.2022:Unity 现已弃用,因此请勿将其用于新项目。我用 Autofac 替换了它。

根据@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());

        }

    }
}

关于c# - 使用 Unity 代替 ASP.Net Core DI IServiceCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50705817/

相关文章:

Azure Web应用程序部署显示hostingstart.html

java - HK2 没有用 jersey 注入(inject) HttpServletRequest

java - 在 C# 和 Java 之间传递 DateTime?

c# - 将 IBM DB2 IXF 文件转换为 CSV 或 XML

c# - 具有约定的内置依赖注入(inject)

c# - 如何使用 Asp.Net Core 2.2 在子域上服务器图像?

interface - 依赖注入(inject)和使用接口(interface)?

asp.net-mvc - 使用 DI ninject 的自定义角色提供程序抛出错误

c# - 读取嵌套的动态 JSON 属性

C#通过FTP下载所有文件和子目录