diff --git a/src/ReC.Client/ReCClient.cs b/src/ReC.Client/ReCClient.cs index 4e4bcb8..138bb72 100644 --- a/src/ReC.Client/ReCClient.cs +++ b/src/ReC.Client/ReCClient.cs @@ -1,4 +1,6 @@ using System.Text.Json; +using Microsoft.Extensions.DependencyInjection; + #if NETFRAMEWORK using System; using System.Net.Http; @@ -42,5 +44,43 @@ namespace ReC.Client var resp = _http.PostAsync($"api/RecAction/invoke/{profileId}", content: null).GetAwaiter().GetResult(); return resp.IsSuccessStatusCode; } + + #region Static + private static readonly IServiceCollection Services = new ServiceCollection(); + + private static IServiceProvider +#if nullable + ? +#endif + ServiceProvider +#if nullable + = null +#endif + ; + + public static void BuildStaticClient(string apiUri) + { + if(ServiceProvider != null) + throw new InvalidOperationException("Static ServiceProvider is already built."); + + Services.AddHttpClient(ClientName, client => + { + client.BaseAddress = new Uri(apiUri); + }); + ServiceProvider = Services.BuildServiceProvider(); + } + + public static ReCClient Static + { + get + { + if (ServiceProvider == null) + throw new InvalidOperationException("Static ServiceProvider is not built. Call BuildStaticClient first."); + + var httpClientFactory = ServiceProvider.GetRequiredService(); + return new ReCClient(httpClientFactory); + } + } + #endregion } } \ No newline at end of file