ow
This commit is contained in:
1 parent
31fbe199c1
commit
1c0c83af5f
469 files changed
+62544
-5
No files matched your search
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user