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