c# – 在Startup.cs之外实现依赖注入
发布时间:2020-12-15 17:42:28 所属栏目:百科 来源:网络整理
导读:我想在ASP.NET CORE 1中实现依赖注入.我知道一切都是关于.Net Core中的DI.例如 public void ConfigureServices(IServiceCollection services) { // Add application services. services.AddTransientIDateTime,SystemDateTime(); } 但是对于拥有20多个实体和
我想在ASP.NET CORE 1中实现依赖注入.我知道一切都是关于.Net Core中的DI.例如
public void ConfigureServices(IServiceCollection services) { // Add application services. services.AddTransient<IDateTime,SystemDateTime>(); } 但是对于拥有20多个实体和服务的大型项目来说,写入ConfigurationServices中的所有代码行是非常困难和不可读的.我想知道这是否可能在Startup.cs之外执行依赖注入,然后将其添加到服务中. 感谢您的答案. 解决方法
您可以编写IServiceCollection的扩展方法,将许多服务注册封装到Startup.cs中的一行代码中
例如这里有一个来自我的项目: using cloudscribe.Core.Models; using cloudscribe.Core.Models.Setup; using cloudscribe.Core.Web; using cloudscribe.Core.Web.Components; using cloudscribe.Core.Web.Components.Editor; using cloudscribe.Core.Web.Components.Messaging; using cloudscribe.Core.Web.Navigation; using cloudscribe.Web.Common.Razor; using cloudscribe.Web.Navigation; using cloudscribe.Web.Navigation.Caching; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Razor; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Options; using System.Reflection; using Microsoft.AspNetCore.Authorization; namespace Microsoft.Extensions.DependencyInjection { public static class StartupExtensions { public static IServiceCollection AddCloudscribeCore(this IServiceCollection services,IConfigurationRoot configuration) { services.TryAddSingleton<IHttpContextAccessor,HttpContextAccessor>(); services.Configure<MultiTenantOptions>(configuration.GetSection("MultiTenantOptions")); services.Configure<SiteConfigOptions>(configuration.GetSection("SiteConfigOptions")); services.Configure<UIOptions>(configuration.GetSection("UIOptions")); services.Configure<CkeditorOptions>(configuration.GetSection("CkeditorOptions")); services.Configure<CachingSiteResolverOptions>(configuration.GetSection("CachingSiteResolverOptions")); services.AddMultitenancy<SiteContext,CachingSiteResolver>(); services.AddScoped<CacheHelper,CacheHelper>(); services.AddScoped<SiteManager,SiteManager>(); services.AddScoped<GeoDataManager,GeoDataManager>(); services.AddScoped<SystemInfoManager,SystemInfoManager>(); services.AddScoped<IpAddressTracker,IpAddressTracker>(); services.AddScoped<SiteDataProtector>(); services.AddCloudscribeCommmon(); services.AddScoped<ITimeZoneIdResolver,RequestTimeZoneIdResolver>(); services.AddCloudscribePagination(); services.AddScoped<IVersionProviderFactory,VersionProviderFactory>(); services.AddScoped<IVersionProvider,CloudscribeCoreVersionProvider>(); services.AddTransient<ISiteMessageEmailSender,SiteEmailMessageSender>(); services.AddTransient<ISmsSender,SiteSmsSender>(); services.AddSingleton<IThemeListBuilder,SiteThemeListBuilder>(); services.TryAddScoped<ViewRenderer,ViewRenderer>(); services.AddSingleton<IOptions<NavigationOptions>,SiteNavigationOptionsResolver>(); services.AddScoped<ITreeCacheKeyResolver,SiteNavigationCacheKeyResolver>(); services.AddScoped<INodeUrlPrefixProvider,FolderTenantNodeUrlPrefixProvider>(); services.AddCloudscribeNavigation(configuration); services.AddCloudscribeIdentity(); return services; } } } 在Startup.cs中,我用一行代码调用该方法 services.AddCloudscribeCore(Configuration); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |