注文処理を管理するためにRESTfulなASP.NET Web APIをデプロイします。 Azure App Services Web Appを開発して、APIを消費し、顧客が商品を注文できるようにします。注文エントリを処理するには、HttpClientオブジェクトを使用します。 Webアプリケーションで大量の同時ユーザーが発生すると、APIによってSocketExceptionエラーが発生します。 エラーを解決する必要があります。 あなたは何をするべきか?
正解:C
Explanation If the class that wraps the external resource is shareable and thread-safe, create a shared singleton instance or a pool of reusable instances of the class. The following example uses a static HttpClient instance, thus sharing the connection across all requests. public class SingleHttpClientInstanceController : ApiController { private static readonly HttpClient httpClient; static SingleHttpClientInstanceController() { httpClient = new HttpClient(); } // This method uses the shared instance of HttpClient for every call to GetProductAsync. public async Task<Product> GetProductAsync(string id) { var hostName = HttpContext.Current.Request.Url.Host; var result = await httpClient.GetStringAsync(string.Format("http://{0}:8080/api/...", hostName)); return new Product { Name = result }; } } References: https://docs.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/