English 中文(简体)
在网络API控制器中使用HttpClientFactory(NET Core 6 )
原标题:Using HttpClientFactory in webAPI controller (.NET core 6 )

我不禁要问,我是否在我的网络中执行过:

<>Startup.cs => Configureservices

services.AddHttpClient("MyHttpClient")

<>Startup.cs => Configure

applicationLifetime.ApplicationStopping.Register(() => {
   MyController.httpClient.Dispose();
});

<><><><><>><>>>><>>>>>>

private static IHttpClientFactory _httpClientFactory;
public static HttpClient httpClient = null;

public MyController(IHttpClientFactory httpClientFactory)
{
     _httpClientFactory = httpClientFactory;

     if (httpClient == null)
     {
         httpClient = _httpClientFactory.CreateClient("MyHttpClient");
     }
}

<>Methods

[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod1() {
   await httpClient.SendAsync() 
   ....
}


[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod2() {
   await httpClient.SendAsync() 
   ....
}
问题回答

我不禁要问,我是否在我的网络中执行我的工作是正确的。

同样,根据你们共同的法典,似乎执行工作将发挥作用。 然而,在少数领域,你可以改进。 例如,你将<海法>海法特准法/代码>作为<条码> 私人静态<条码>,但所建议的做法是:<条码> 仅读,我不肯定,为什么你与静态编码脱钩,你为什么想要这种变数的价值只有在固定构造中才能改变。

另一个重要要点是,正如同侪呼吁应当涉及任务,而><>/code><>a>是自然做法,因为如果你使用codesync >,则具有重大不利之处,并且可以限制您本人的请求。 正因为如此,永远不会鼓励使用 void。 可在

Finally, I fouond one inconsistency but its minor, however, I am little scrupulous here in this line, if you define HttpClient httpClient = null that means, you want this as nullable, so this should be as following:

public readonly HttpClient? httpClient = null;

因此,我离开了<代码>HttpClient?,作为无效,因此,在我上任的<代码>null 转让。

因此,我认为,你的全面执行可以改进如下:

public class MyController : Controller
    {

        private readonly IHttpClientFactory _httpClientFactory;
        public readonly HttpClient? httpClient = null;

        public MyController(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        [HttpGet]
        [Consumes("application/x-www-form-urlencoded")]
        public async Task MyMethod1()
        {
            await httpClient.SendAsync();
        }
        
    }

Apart from above implementation, I would like to inform, depending on the requirement there are several ways for IHttpClientFactory you could consier.

例如,

<>说明: 请refer to this official document,如果你想要知道





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签