AutoDispatching/AutoDispathingWork/Utils/StaticServiceProvider.cs
2023-11-19 17:06:38 +08:00

34 lines
805 B
C#

using LiteDB;
using WorkerService1.Domains;
namespace AutoDispathingWork.Utils;
public class StaticServiceProvider
{
public static void SetServiceProvider(IServiceProvider serviceProvider)
{
Current = serviceProvider;
}
private static IServiceProvider Current { get; set; }
public static T GetRequiredService<T>()
{
return Current.GetRequiredService<T>();
}
public static T? GetService<T>()
{
return Current.GetService<T>();
}
public static ILiteCollection<LogInfo> GetLogDb()
{
return Current.GetRequiredService<LiteDatabase>().GetCollection<LogInfo>();
}
public static ILogger GetLogger(string methodName)
{
return Current.GetRequiredService<ILoggerFactory>().CreateLogger(methodName);
}
}