using System;
using System.Text;
using System.Threading.Tasks;
using DS.Module.Core.Exceptions;
using DS.Module.Core.Modules;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace DS.Module.Core.Extensions;
public static partial class Extensions
{
///
/// 获取指定类型的日志对象
///
/// 非静态强类型
/// 日志对象
public static ILogger GetLogger(this IServiceProvider provider)
{
ILoggerFactory factory = provider.GetService();
return factory.CreateLogger();
}
///
///
///
///
///
///
public static ILogger GetLogger(this IServiceProvider provider, Type type)
{
ILoggerFactory factory = provider.GetService();
return factory.CreateLogger(type);
}
// public static AppOptionSettings GetAppSettings(this IServiceProvider provider)
// {
// provider.NotNull(nameof(provider));
// return provider.GetService>()?.Value;
// }
public static object GetInstance(this IServiceProvider provider, ServiceDescriptor descriptor)
{
if (descriptor.ImplementationInstance != null)
{
return descriptor.ImplementationInstance;
}
if (descriptor.ImplementationType != null)
{
return provider.GetServiceOrCreateInstance(descriptor.ImplementationType);
}
return descriptor.ImplementationFactory(provider);
}
///
/// 获取日志记录器
///
///
///
///
public static ILogger GetLogger(this ILazyServiceProvider provider, Type type)
{
ILoggerFactory factory = provider.LazyGetService();
return factory.CreateLogger(type);
}
public static object GetServiceOrCreateInstance(this IServiceProvider provider, Type type)
{
return ActivatorUtilities.GetServiceOrCreateInstance(provider, type);
}
public static object CreateInstance(this IServiceProvider provider, Type type, params object[] arguments)
{
return ActivatorUtilities.CreateInstance(provider, type, arguments);
}
public static void GetService(this IServiceProvider provider, Action action)
{
action.NotNull(nameof(action));
var t = provider.GetService();
action(t);
}
///
///创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static void CreateScoped(this IServiceProvider provider, Action callback)
{
using var scope = provider.CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
callback(service, scope.ServiceProvider.GetRequiredService());
if (service is IDisposable disposable)
{
disposable.Dispose();
}
}
///
/// 创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static void CreateScoped(this IServiceProvider provider, Action callback)
{
using var scope = provider.CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
callback(service);
if (service is IDisposable disposable)
{
disposable.Dispose();
}
}
///
/// 创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static T CreateScoped(this IServiceProvider provider, Func callback)
{
using var scope = provider.CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
return callback(service);
}
///
/// 创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static async Task CreateScopedAsync(this IServiceProvider provider, Func callback)
{
using var scope = provider.GetRequiredService().CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
await callback(service, scope.ServiceProvider.GetRequiredService());
if (service is IDisposable disposable)
{
disposable.Dispose();
}
}
///
/// 创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static async Task CreateScopedAsync(this IServiceProvider provider, Func callback)
{
using var scope = provider.GetRequiredService().CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
await callback(service);
if (service is IDisposable disposable)
{
disposable.Dispose();
}
}
///
///创建一个IServiceScope,其中包含一个IServiceProvider,用于从新创建的作用域解析依赖项,然后运行关联的回调。
///
public static async Task CreateScopedAsync(this IServiceProvider provider, Func> callback)
{
using var scope = provider.GetRequiredService().CreateScope();
var service = scope.ServiceProvider.GetRequiredService();
return await callback(service);
}
public static void CreateScoped(this IServiceProvider provider, Action callback)
{
using var scope = provider.CreateScope();
callback(scope.ServiceProvider);
}
#region 读取文件
///
/// 得到文件容器
///
/// 服务接口
/// 文件名+后缀名
/// 文件不存提示信息
/// 返回文件中的文件
public static string GetFileText(this IServiceProvider provider, string fileName, string fileNotExistsMsg)
{
fileName.NotNullOrEmpty(nameof(fileName));
var fileProvider = provider.GetService();
fileProvider.NotNull(nameof(fileProvider));
var fileInfo = fileProvider.GetFileInfo(fileName);
if (!fileInfo.Exists)
{
if (!fileNotExistsMsg.IsNullOrEmpty())
{
throw new DSAppException(fileNotExistsMsg);
}
}
var text = ReadAllText(fileInfo);
if (text.IsNullOrEmpty())
{
throw new DSAppException("文件内容不存在");
}
return text;
}
///
/// 根据配置得到文件内容
///
/// 服务接口
/// 分区键
/// 文件不存提示信息
/// 返回文件中的文件
public static string GetFileByConfiguration(this IServiceProvider provider, string sectionKey,
string fileNotExistsMsg)
{
sectionKey.NotNullOrEmpty(nameof(sectionKey));
var configuration = provider.GetService();
var value = configuration?.GetSection(sectionKey)?.Value;
return provider.GetFileText(value, fileNotExistsMsg);
}
///
/// 读取全部文本
///
/// 文件信息接口
///
private static string ReadAllText(IFileInfo fileInfo)
{
byte[] buffer;
using var stream = fileInfo.CreateReadStream();
buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return Encoding.Default.GetString(buffer).Trim();
}
#endregion
}