54 lines
1.8 KiB
C#
54 lines
1.8 KiB
C#
using CloudBuilder.Core.DependencyInjection.Form;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace CloudBuilder
|
|
{
|
|
public class ServiceHelper
|
|
{
|
|
|
|
//services.TryAddSingleton<ApplicationServiceContext>();
|
|
/// <summary>
|
|
/// 通过单例获取指定类型的服务
|
|
/// </summary>
|
|
public static R GetService<R>()
|
|
{
|
|
return ApplicationServiceContext.ServiceProvider.GetService<R>()!;
|
|
}
|
|
|
|
//services.TryAddSingleton<IContext, LoginContext>();
|
|
/// <summary>
|
|
/// 通过接口获取指定类型的服务
|
|
/// </summary>
|
|
public static R GetInterfaceService<R>()
|
|
{
|
|
return ApplicationServiceContext.ServiceProvider.GetService<R>()!;
|
|
}
|
|
|
|
//services.TryAddKeyedScoped(implementType, implementType.Name);
|
|
/// <summary>
|
|
/// 通过名称获取指定类型的服务
|
|
/// </summary>
|
|
public static R GetTypeService<R>()
|
|
{
|
|
return ApplicationServiceContext.ServiceProvider.GetRequiredKeyedService<R>(typeof(R).Name);
|
|
}
|
|
|
|
//services.TryAddKeyedScoped(implementType.BaseType, implementType.Name, implementType);
|
|
/// <summary>
|
|
/// 通过名称获取指定类型(被继承的类)的服务
|
|
/// </summary>
|
|
public static R GetBaseTypeService<R>(string name)
|
|
{
|
|
return ApplicationServiceContext.ServiceProvider.GetRequiredKeyedService<R>(name);
|
|
}
|
|
|
|
public static R GetBaseTypeScopeService<R>(string name)
|
|
{
|
|
using (var scope = ApplicationServiceContext.ServiceProvider.CreateScope())
|
|
{
|
|
return scope.ServiceProvider.GetRequiredKeyedService<R>(name);
|
|
}
|
|
}
|
|
}
|
|
}
|