using System;
using Microsoft.Extensions.DependencyInjection;
using AyaNova.Models;
namespace AyaNova.Util
{
///
/// Shared service provider for static classes
///
internal static class ServiceProviderProvider
{
private static IServiceProvider _provider;
//CALL IT LIKE THIS:
// ApiServerState apiServerState = (ApiServerState)ServiceProviderProvider.Provider.GetService(typeof(ApiServerState));
/*
or is it like this??
using (IServiceScope scope = provider.CreateScope())
{
AyContext ct = scope.ServiceProvider.GetRequiredService();
ApiServerState serverState = scope.ServiceProvider.GetRequiredService();
*/
internal static IServiceProvider Provider
{
get
{
#if (DEBUG)
if (_provider == null)
{
throw new System.NotSupportedException("ServiceProviderProvider.cs - Attempt to use service provider before it's been initialized");
}
#endif
return _provider;
}
set
{
_provider = value;
}
}
internal static IServiceScope Scope
{
get
{
return Provider.CreateScope();
}
}
internal static AyContext DBContext
{
get
{
return Scope.ServiceProvider.GetRequiredService();
}
}
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1
internal static System.Net.Http.IHttpClientFactory HttpClientFactory
{
get
{
return Scope.ServiceProvider.GetRequiredService();
}
}
}
}