AutoDispatching/AutoDispathingWork/Utils/StaticServiceProvider.cs
2024-08-10 23:43:46 +08:00

39 lines
977 B
C#

using LiteDB;
using Microsoft.EntityFrameworkCore;
using WorkerService1;
using WorkerService1.Domains;
namespace AutoDispathingWork.Utils;
public class StaticServiceProvider
{
public static void SetServiceProvider(IServiceProvider serviceProvider)
{
Current = serviceProvider;
}
public 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 void AddLog(LogInfo log)
{
using var scope = Current.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
dbContext.LogInfos.Add(log);
dbContext.SaveChanges();
}
public static ILogger GetLogger(string methodName)
{
return Current.GetRequiredService<ILoggerFactory>().CreateLogger(methodName);
}
}