AutoDispatching/AutoDispathingWork/Utils/StaticServiceProvider.cs
2025-11-05 20:52:03 +08:00

38 lines
927 B
C#

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);
}
}