前面分享了.net core Program类的启动过程已经源代码介绍,这里将继续讲Startup类中的两个约定方法,一个是ConfigureServices,这个方法是用来写咱们应用程序所依赖的组件。另外一个Configure,它是咱们MVC请求的中间件方法,也就是咱们每一个请求来要执行的过程均可以写在这个方法里面。
为何说Startup类中的两个方法是基于约定的呢?实际上是这样的,在.net core Program类Main方法中有个调用了Run方法这个方法从IServiceCollection容器中拿到一个IStartup类型的实例而后调用了IStartup中定义的两个方法法,若是咱们的Startup类是实现了这个接口的类 那么就不是基于约定了,直接就可使用,可是咱们发如今vs给咱们生成的Startup类并无实现任何接口,因此就不会是IStartup类型,那么内部是如何去作的呢? 实际上是这样的,在注册Startup实例的时候还有个类型叫作ConventionBasedStartup从名称上解读这个类就是转换为基础的Startup,其实倒是也是这样的,这个类中是实现了IStartup接口,它的两个方法中分别调用了各自的对用委托,这些委托实际执行的就是咱们Startup类中定义的两个方法,请看源代码:html
public class ConventionBasedStartup : IStartup { private readonly StartupMethods _methods; public ConventionBasedStartup(StartupMethods methods) { _methods = methods; } public void Configure(IApplicationBuilder app) { try { _methods.ConfigureDelegate(app); } catch (Exception ex) { if (ex is TargetInvocationException) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } throw; } } public IServiceProvider ConfigureServices(IServiceCollection services) { try { return _methods.ConfigureServicesDelegate(services); } catch (Exception ex) { if (ex is TargetInvocationException) { ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } throw; } } }
如今Startup类的方法说清楚了,咱们具体来讲说方法中的内容,首先说ConfigureServices(IServiceCollection services),这个方法的参数是约定好的,不能随意改变,里面的IServiceCollection接口其实就是咱们依赖注入的容器,说的再直白一点就是咱们整个MVC所须要的实例都由IServiceCollection所管理,IServiceCollection有几个重要的扩展方法,他们都是定义在ServiceCollectionServiceExtensions静态类中,AddTransient方法,表示用这个方法添加到IServiceCollection容器的实例在须要注入的实例中都是一个全新的实例,AddScoped方法,这个方法表示在一次请求的生命周期内共用一个实例,AddSingleton方法,这个方法表示整个程序共用一个实例,例如日志服务,IConfiguration服务等都属于典型Singleton。请看例子:app
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddTransient<TransientService>(); services.AddTransient<ServiceDemo1>(); services.AddTransient<ServiceDemo2>(); } public void Configure(IApplicationBuilder app, ServiceDemo1 demo1, ServiceDemo2 demo2 ) { demo1.Test(); demo2.Test(); app.Run(async (HttpContext context) => { await context.Response.WriteAsync("test successd"); }); } } public class TransientService { private int _updateCount; public int GetUpdateCount() {
this._updateCount = this._updateCount + 1; return this._updateCount; } } public class ServiceDemo1 { private readonly TransientService _service; public ServiceDemo1(TransientService service) { _service = service; } public void Test() { Console.WriteLine($"我是demo1的计数:{this._service.GetUpdateCount()}"); } } public class ServiceDemo2 { private readonly TransientService _service; public ServiceDemo2(TransientService service) { _service = service; } public void Test() { Console.WriteLine($"我是demo2的计数:{this._service.GetUpdateCount()}"); } }
上面的例子中会产生一下结果,能够看得出来这两个注入的TransientService都是全新的实例async
若是咱们稍微改变一下注入的方法,将本来的 services.AddTransient<TransientService>();改为services.AddScoped<TransientService>();就会产生以下结果:ide
这个能说明什么呢,咱们有两次注入 这个就表示TransientService保持了以前demo1的状态 demo1和demo2是能够共用这个实例来传输数据的,AddSingleton方法理解起来比较简单就不过多絮叨了,上面已经说明。ui
接下来再来讲说Startup类中的Configure方法,Configure方法中的参数是能够变化的,也就是说你能够用依赖注入的方法在参数中注入你想要注入的实例,前面说了 这个方法是咱们请求的中间件方法,这个方法中会整合咱们注入的IApplicationBuilder 中调用的各类Use方法中定义的中间件 并非说这里面定义的代码每次请求都会被执行,这个概念必定要搞清楚,Configure方法只会在启动的时候执行一次,后面就不会再执行了,Configure方法只是让咱们能够定义整个MVC的处理请求的执行顺序,具体的能够看看官方的文档https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.2
this
其实中间件都是由IApplicationBuilder 所管理的ApplicationBuilder类实现了IApplicationBuilder 接口中的方法,看到ApplicationBuilder中的源代码中有个属性_components 它是一个IList<Func<RequestDelegate, RequestDelegate>>类型的委托容器,容器中的委托就是请求过来须要执行的中间件委托,当你在Configure方法中调用app.UseXXX的时候就会被注册到这个容器中去,而后请求过来就按照顺序执行容器中的每个委托,因此这里就解释了前面说的Configure方法只会被执行一次的说法。下面也贴一下ApplicationBuilder类的源代码:spa
public class ApplicationBuilder : IApplicationBuilder { private readonly IList<Func<RequestDelegate, RequestDelegate>> _components = new List<Func<RequestDelegate, RequestDelegate>>(); public IServiceProvider ApplicationServices { get { return GetProperty<IServiceProvider>(Constants.BuilderProperties.ApplicationServices); } set { SetProperty(Constants.BuilderProperties.ApplicationServices, value); } } public IFeatureCollection ServerFeatures => GetProperty<IFeatureCollection>(Constants.BuilderProperties.ServerFeatures); public IDictionary<string, object> Properties { get; } public ApplicationBuilder(IServiceProvider serviceProvider) { Properties = new Dictionary<string, object>(StringComparer.Ordinal); ApplicationServices = serviceProvider; } public ApplicationBuilder(IServiceProvider serviceProvider, object server) : this(serviceProvider) { SetProperty(Constants.BuilderProperties.ServerFeatures, server); } private ApplicationBuilder(ApplicationBuilder builder) { Properties = new CopyOnWriteDictionary<string, object>(builder.Properties, StringComparer.Ordinal); } private T GetProperty<T>(string key) { if (!Properties.TryGetValue(key, out object value)) { return default(T); } return (T)value; } private void SetProperty<T>(string key, T value) { Properties[key] = value; } public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware) { _components.Add(middleware); return this; } public IApplicationBuilder New() { return new ApplicationBuilder(this); } public RequestDelegate Build() { RequestDelegate requestDelegate = delegate (HttpContext context) { context.Response.StatusCode = 404; return Task.CompletedTask; }; foreach (Func<RequestDelegate, RequestDelegate> item in _components.Reverse()) { requestDelegate = item(requestDelegate); } return requestDelegate; } }
好啦,这篇关于Startup类就算介绍完成了,下篇开始正式介绍MVC.net