public RestClient(RestClientSettings settings, string clientName = null, object appContext = null, CancellationToken?cancellationToken = null, HttpClient httpClient = null) { RestClientSettings.Validate(settings); Settings = settings; AppContext = appContext; ClientName = clientName; if (cancellationToken.HasValue) { CancellationToken = cancellationToken.Value; } // In Web environment (Asp.NET core) the HttpClient should be created using IHttpClientFactory // https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2 // In this case create client through this factory and pass it here if (httpClient != null) { this.HttpClient = httpClient; } else { // create global singleton handler if not created yet SharedHttpClientHandler = SharedHttpClientHandler ?? new HttpClientHandler(); HttpClient = new HttpClient(SharedHttpClientHandler); } }
internal static void Validate(RestClientSettings settings) { ThrowIf(settings == null, "Settings parameter may not be null."); ThrowIf(settings.Serializer == null, "Settings.Serializer property may not be null."); ThrowIf(string.IsNullOrWhiteSpace(settings.ServiceUrl), "ServiceUrl may not be empty."); }