具有HttpClientFactory实现的动态代理 [英] Having dynamic proxy with HttpClientFactory implementation

查看:179
本文介绍了具有HttpClientFactory实现的动态代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Asp.Net Core WebApi.我根据 HttpClientFactory发出Http请求模式.这是我的示例代码:

I have Asp.Net Core WebApi. I am making Http requests according to HttpClientFactory pattern. Here is my sample code:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddHttpClient<IMyInterface, MyService>();
    ...
}

public class MyService: IMyInterface
{
    private readonly HttpClient _client;

    public MyService(HttpClient client)
    {
        _client = client;
    }

    public async Task CallHttpEndpoint()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com");
        var response = await _client.SendAsync(request);
        ...
    }

}

我想通过动态代理实现发送请求.这基本上意味着我可能需要为每个请求更改代理.到目前为止,我发现了2种解决方案,对我来说,其中的一种似乎不是很好:

I want to implement sending requests through dynamic proxy. This basically means that I might need to change proxy with each request. As for now I find out 2 approuces, non of which seems good to me:

1.拥有一个像这样的静态代理:

1.Have a static proxy like this:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddHttpClient<IMyInterface, MyService>().ConfigurePrimaryHttpMessageHandler(() =>
        {
            return new HttpClientHandler
            {
                Proxy = new WebProxy("http://127.0.0.1:8888"),
                UseProxy = true
            };
        });
    ...
}

但是在这种方法中,每个服务只能有一个代理.

But I can only have single proxy per service in this approach.

2.将每个请求处置HttpClient:

2.Dispose HttpClient with each request:

    HttpClientHandler handler = new HttpClientHandler()
    {
        Proxy = new WebProxy("http://127.0.0.1:8888"),
        UseProxy = true,
    };

    using(var client = new HttpClient(handler))
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com");
        var response = await client.SendAsync(request);
        ...
    }

但是以这种方式,我违反了HttpClientFactory模式,并且可能导致应用程序性能出现问题,如以下文章

But in this way I violate HttpClientFactory pattern and it might cause issues to application performance as stated in following article

还有第三种方法可以在不重新创建HttpClient的情况下动态更改代理吗?

Is there a third way where I could change proxy dinamically without re-creating HttpClient?

推荐答案

无法更改HttpClientHandler的任何属性,也不能在其后为现有的HttpClient分配新版本的HttpClientHandler.被实例化.这样,就不可能为特定的HttpClient创建动态代理:您只能指定一个代理.

There is no way to change the any of the properties of HttpClientHandler or to assign a new version of HttpClientHandler to an existing HttpClient after it is instantiated. As such, it is then impossible to have a dynamic proxy for a particular HttpClient: you can only specify one proxy.

实现此目的的正确方法是改为使用命名客户端,并为每个代理端点定义一个客户端.然后,您需要注入IHttpClientFactory并选择要使用的代理之一,请求实现该代理的命名客户端.

The correct way to achieve this is to use named clients, instead, and define a client for each proxy endpoint. Then, you'll need to inject IHttpClientFactory and pick one of the proxies to use, requesting the named client that implements that.

services.AddHttpClient("MyServiceProxy1").ConfigurePrimaryHttpMessageHandler(() =>
{
    return new HttpClientHandler
    {
        Proxy = new WebProxy("http://127.0.0.1:8888"),
        UseProxy = true
    };
});

services.AddHttpClient("MyServiceProxy2").ConfigurePrimaryHttpMessageHandler(() =>
{
    return new HttpClientHandler
    {
        Proxy = new WebProxy("http://127.0.0.1:8889"),
        UseProxy = true
    };
});

...

然后:

public class MyService : IMyInterface
{
    private readonly HttpClient _client;

    public MyService(IHttpClientFactory httpClientFactory)
    {
        _client = httpClientFactory.CreateClient("MyServiceProxy1");
    }

    public async Task CallHttpEndpoint()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "www.customUrl.com");
        var response = await _client.SendAsync(request);
        ...
    }
}

这篇关于具有HttpClientFactory实现的动态代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆