如何在类库中使用ihttpclientFactory

发布于 2025-01-24 11:19:15 字数 220 浏览 3 评论 0原文

我正在尝试将一些代码从Net Core API转换为类库。

我被卡住了如何使用httpclientFactory。 通常,可以在program.cs或启动中配置httpclientfactory,例如

services.AddHttpClient("abc", xxx config). 

如何在类库中为httpclientFactory进行配置。

I am trying to convert some code from net core api to class library.

I am stuck how to use HttpClientfactory.
Normally the httpclientfactory can be configured in program.cs or Startup like

services.AddHttpClient("abc", xxx config). 

How to do configurations in class library for Httpclientfactory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2025-01-31 11:19:15

您可以按以下尝试尝试:

public class HttpClientUtil
    {
        private static IServiceProvider serviceProvider { get; set; } 

        public static void Initial(IServiceProvider Provider)
        {
            if (Provider == null)
            {
                IHostBuilder builder = Host.CreateDefaultBuilder();
                builder.ConfigureServices(services =>
                {
                    services.AddHttpClient("client_1", config =>
                    {
                        config.BaseAddress = new Uri("http://client_1.com");
                        config.DefaultRequestHeaders.Add("header_1", "header_1");
                    });
                    services.AddHttpClient("client_2", config =>
                    {
                        config.BaseAddress = new Uri("http://client_2.com");
                        config.DefaultRequestHeaders.Add("header_2", "header_2");
                    });
                });
                serviceProvider = builder.Build().Services;
            }
            else
            {
                serviceProvider = Provider;
            } 
        }

        public static IHttpClientFactory GetHttpClientFactory()
        {
            if(serviceProvider==null)
            {
                Initial(serviceProvider);
            }
            return (IHttpClientFactory)serviceProvider.GetServices<IHttpClientFactory>();
        }

    }

您可以通过接口获得HTTPClinetFactory的实例

You could try as below:

public class HttpClientUtil
    {
        private static IServiceProvider serviceProvider { get; set; } 

        public static void Initial(IServiceProvider Provider)
        {
            if (Provider == null)
            {
                IHostBuilder builder = Host.CreateDefaultBuilder();
                builder.ConfigureServices(services =>
                {
                    services.AddHttpClient("client_1", config =>
                    {
                        config.BaseAddress = new Uri("http://client_1.com");
                        config.DefaultRequestHeaders.Add("header_1", "header_1");
                    });
                    services.AddHttpClient("client_2", config =>
                    {
                        config.BaseAddress = new Uri("http://client_2.com");
                        config.DefaultRequestHeaders.Add("header_2", "header_2");
                    });
                });
                serviceProvider = builder.Build().Services;
            }
            else
            {
                serviceProvider = Provider;
            } 
        }

        public static IHttpClientFactory GetHttpClientFactory()
        {
            if(serviceProvider==null)
            {
                Initial(serviceProvider);
            }
            return (IHttpClientFactory)serviceProvider.GetServices<IHttpClientFactory>();
        }

    }

you could get the instance of httpclinetfactory through the interface

最近可好 2025-01-31 11:19:15

在您的库中,添加了iservCollection的扩展方法,以在主项目中“启用”它。

在库中:

public static class ServiceCollectionExt
{
    public static void AddYourStaff(this IServiceCollection services)
    {
        services.AddHttpClient("xxx", client =>
        {
            //your staff here
        });
        services.AddSingleton<ISomethingElse, SomethingElse>();
    }
} 

然后在您的启动中只需调用它:

services.AddYourStaff();

更新:如作者所述,他正在基于插件的应用程序。例如,在这种情况下,您需要某种惯例:

  1. 每个插件库都必须具有一个公共静态类,称为registration使用公共静态方法Invoke(IserviceCollection SC,iconFiguration config)
  2. 然后,在您的启动中,您可以通过所有插件库迭代其registration.invoke(sc,config)使用反射:
foreach(var pluginAssembly in plugins)
{
    pluginAssembly
        .GetType("Registration")
        .GetMethod("Invoke", BindingFlags.Public | BindingFlags.Static)
        .Invoke(null, new object[] {services, Configuration});
}

In your library add an extension method for IServiceCollection to "enable" it in the main project.

In the library:

public static class ServiceCollectionExt
{
    public static void AddYourStaff(this IServiceCollection services)
    {
        services.AddHttpClient("xxx", client =>
        {
            //your staff here
        });
        services.AddSingleton<ISomethingElse, SomethingElse>();
    }
} 

Then in your Startup just call it:

services.AddYourStaff();

UPDATE: As the author described, he's working on the plugin based application. In that case you need some kind of convention, for instance:

  1. each plugin library must have a public static class called Registration with public static method Invoke(IServiceCollection sc, IConfiguration config)
  2. Then in your Startup you can iterate through all plugin libraries and call their Registration.Invoke(sc, config) using reflection:
foreach(var pluginAssembly in plugins)
{
    pluginAssembly
        .GetType("Registration")
        .GetMethod("Invoke", BindingFlags.Public | BindingFlags.Static)
        .Invoke(null, new object[] {services, Configuration});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文