public static IEnumerable <SmartServiceDescriptor> GetServices(SchubertWebOptions options, bool append = true) { Guard.ArgumentNotNull(options, nameof(options)); if (append) { yield return(ServiceDescriber.Transient <IModuleFinder, PackageFinder>(SmartOptions.Append)); yield return(ServiceDescriber.Singleton <IWorkContextProvider, HttpWorkContextProvider>(SmartOptions.Append)); yield return(ServiceDescriber.Transient <IShellBlueprintItemExporter, ControllerExporter>(SmartOptions.Append)); } yield return(ServiceDescriber.Singleton <IHttpContextAccessor, HttpContextAccessor>()); yield return(ServiceDescriber.Scoped <HttpWorkContext, HttpWorkContext>()); yield return(ServiceDescriber.Transient <ISchubertEnvironment, AspNetEnvironment>()); yield return(ServiceDescriber.Transient <ICookiesAccessor, CookiesAccessor>()); yield return(ServiceDescriber.Scoped <IClientEnvironment, ClientEnvironment>()); if (options.MvcFeatures != MvcFeatures.None) { yield return(ServiceDescriber.Transient <IApplicationModelProvider, SchubertApplicationModeProvider>(SmartOptions.TryAppend)); if (options.MvcFeatures == MvcFeatures.Full) { yield return(ServiceDescriber.Scoped <IHtmlSegmentManager, HtmlSegmentManager>()); } } }
/// <summary> /// 启用Schubert 框架的 Web 特性。 /// </summary> /// <param name="services"></param> /// <param name="setup">加入 Mvc 支持。</param> /// <returns></returns> public static SchubertServicesBuilder AddWebFeature(this SchubertServicesBuilder services, Action <SchubertWebBuilder> setup = null) { SchubertWebOptions options = new SchubertWebOptions(); bool firstInvoke = true; if ((firstInvoke = services.AddedModules.Add(_module))) { IConfiguration configuration = services.Configuration.GetSection("Schubert:Web") as IConfiguration ?? new ConfigurationBuilder().Build(); services.ServiceCollection.Configure <SchubertWebOptions>(configuration); var schubertWebSetup = new SchubertWebOptionsSetup(configuration); schubertWebSetup.Configure(options); } _webBuilder = new SchubertWebBuilder(services); setup?.Invoke(_webBuilder); if (_webBuilder.FeatureSetup != null) { services.ServiceCollection.Configure(setup); } _webBuilder.FeatureSetup?.Invoke(options); services.ServiceCollection.AddDataProtection(); services.ServiceCollection.AddLocalization(); services.ServiceCollection.Replace(ServiceDescriptor.Singleton <IStringLocalizerFactory, SchubertStringLocalizerFactory>()); services.ServiceCollection.TryAddSingleton <IMemoryCache>(s => LocalCache.InnerCache); services.AddCacheForAspNet(); var cookieSetup = _webBuilder.CookieSetup; services.ServiceCollection.ConfigureApplicationCookie(o => { o.LoginPath = "/Login"; o.LogoutPath = "/LogOff"; o.Cookie.HttpOnly = true; o.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest; cookieSetup?.Invoke(o); }); var authenticationBuilder = services.ServiceCollection.AddAuthentication(); if (options.UseCookieAuth) { authenticationBuilder.AddCookie(); } if (options.UseSession) { services.ServiceCollection.AddSession(sop => { sop.IdleTimeout = TimeSpan.FromMinutes(options.SessionTimeoutMinutes); }); } services.ServiceCollection.AddSmart(SchubertWebServices.GetServices(options, firstInvoke)); foreach (var s in _webBuilder.WebStarters) { s.ConfigureServices(services, options); } AddMvc(services, _webBuilder, options); return(services); }
private static void AddMvc(SchubertServicesBuilder services, SchubertWebBuilder featureBuilder, SchubertWebOptions options) { Action <MvcOptions> configure = mvc => { if (!options.GlobalRoutePrefix.IsNullOrWhiteSpace()) { mvc.Conventions.Insert(0, new RoutePrefixConvention(new RouteAttribute(options.GlobalRoutePrefix.Trim()))); } }; switch (options.MvcFeatures) { case MvcFeatures.Full: var mvcBuilder = services.ServiceCollection.AddMvc(configure); featureBuilder.MvcSetup?.Invoke(mvcBuilder); mvcBuilder.AddJsonOptions(json => json.SerializerSettings.ContractResolver = GetContractResolver(options.JsonCaseStyle, options.JsonResolver)) .AddRazorOptions(rveo => { rveo.FileProviders.Insert(0, new ModuleFileProvider(rveo.FileProviders.FirstOrDefault())); rveo.ViewLocationExpanders.Insert(0, new ModuleViewLocationExpander()); }); //services.ServiceCollection.AddAntiforgery(); break; case MvcFeatures.Core: var coreBuilder = services.ServiceCollection.AddMvcCore(configure); featureBuilder.MvcCoreSetup?.Invoke(coreBuilder); break; case MvcFeatures.Api: var apiBuilder = services.ServiceCollection.AddMvcCore(configure); featureBuilder.MvcCoreSetup?.Invoke(apiBuilder); apiBuilder.AddApiExplorer() .AddAuthorization() .AddFormatterMappings() .AddJsonFormatters(settings => settings.ContractResolver = GetContractResolver(options.JsonCaseStyle, options.JsonResolver)) .AddDataAnnotations() .AddCors(); featureBuilder.AddWebApiConventions(); break; default: return; } }