public async Task StartAsync() { using (var scope = AutofacContainer.BeginLifetimeScope()) { var commonConfig = scope.Resolve <ICommonConfig>(); var logTrace = scope.Resolve <ILogTrace>(); logTrace.Push(LogLevel.Info, "title 1", "hello"); logTrace.Push(LogLevel.Warning, "title 2", new { A = "b", C = "d" }); logTrace.Flush(); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); //var servicenamed = this.AutofacContainer.Resolve<IMyService>(); //servicenamed.ShowCode(); //var service = this.AutofacContainer.ResolveNamed<IMyService>("service2"); //service.ShowCode(); #region ×ÓÈÝÆ÷ using(var myscope = AutofacContainer.BeginLifetimeScope("myscope")) { var service0 = myscope.Resolve<MyNameService>(); using (var scope = myscope.BeginLifetimeScope()) { var service1 = scope.Resolve<MyNameService>(); var service2 = scope.Resolve<MyNameService>(); Console.WriteLine($"service1 = service2: {service1 == service2}"); Console.WriteLine($"service1=service0: {service1 == service0}"); } } #endregion if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // Defines the middleware for the request pipeline public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { #region 对中间件管道进行分支.Map 扩展用作约定来创建管道分支。Map 基于给定请求路径的匹配项来创建请求管道分支。 如果请求路径以给定路径开头,则执行分支。 app.Map("/map1", HandleMapTest1); app.Map("/map2", HandleMapTest2); //Map 支持嵌套 app.Map("/level1", level1App => { level1App.Map("/level2a", level2AApp => { // "/level1/level2a" processing }); level1App.Map("/level2b", level2BApp => { // "/level1/level2b" processing }); }); //Map 还可同时匹配多个段 app.Map("/map1/seg1", HandleMultiSeg); //MapWhen 基于给定谓词的结果创建请求管道分支 app.MapWhen(context => context.Request.Query.ContainsKey("branch"), HandleBranch); #endregion //UseWhen 也基于给定谓词的结果创建请求管道分支。 与 MapWhen 不同的是,如果这个分支不发生短路或包含终端中间件,则会重新加入主管道 app.UseWhen(context => context.Request.Query.ContainsKey("branch"), HandleBranchAndRejoin); #region autofac 解析出对象 this.AutofacContainer = app.ApplicationServices.GetAutofacRoot(); var servicenamed = this.AutofacContainer.Resolve <IMyService>(); servicenamed.ShowCode(); var service = this.AutofacContainer.ResolveNamed <IMyService>("service2"); service.ShowCode(); #region 子容器 using (var myscope = AutofacContainer.BeginLifetimeScope("myscope")) { var service0 = myscope.Resolve <MyNameService>(); using (var scope = myscope.BeginLifetimeScope()) { var service1 = scope.Resolve <MyNameService>(); var service2 = scope.Resolve <MyNameService>(); Console.WriteLine($"service1=service2:{service1 == service2}"); Console.WriteLine($"service1=service0:{service1 == service0}"); } } #endregion #endregion //中间件顺序 //https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1 // 注入中间件 处理HttpContext请求 Console.WriteLine("5 Startup.Configure"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); //app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseWebSockets(); //要提供默认文件,必须在 UseStaticFiles 前调用 UseDefaultFiles。 //UseDefaultFiles 实际上用于重写 URL,不提供文件。 //通过 UseStaticFiles 启用静态文件中间件来提供文件。 app.UseDefaultFiles(); //在请求的文件夹中搜索default.htm、 default.html、index.htm、index.html //http://<server_address>/StaticFiles DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("mydefault.html");// 将mydefault.html设置为默认页 app.UseDefaultFiles(options); ////wwwroot // //css // //images // //js ////MyStaticFiles // //images // //banner1.svg app.UseStaticFiles(); // For the wwwroot folder //< img src = "~/images/banner1.svg" alt = "ASP.NET" class="img-responsive" /> //等价于 wwwroot/images/banner1.svg //http://<server_address>/StaticFiles/images/banner1.svg 前端请求的地址 //应用重定向StaticFiles到MyStaticFiles //< img src = "~/StaticFiles/images/banner1.svg" alt = "ASP.NET" class="img-responsive" /> //等价于 MyStaticFiles/images/banner1.svg var cachePeriod = env.IsDevelopment() ? "600" : "604800"; var provider = new FileExtensionContentTypeProvider(); // Add new mappings provider.Mappings[".myapp"] = "application/x-msdownload"; provider.Mappings[".htm3"] = "text/html"; provider.Mappings[".image"] = "image/png"; // Replace an existing mapping provider.Mappings[".rtf"] = "application/x-msdownload"; // Remove MP4 videos. provider.Mappings.Remove(".mp4"); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")), RequestPath = "/StaticFiles", OnPrepareResponse = ctx => { // Requires the following import: // using Microsoft.AspNetCore.Http; ctx.Context.Response.Headers.Append("Cache-Control", $"public, max-age={cachePeriod}"); }, ContentTypeProvider = provider //静态文件授权 //var file = Path.Combine(Directory.GetCurrentDirectory(), // "MyStaticFiles", "images", "banner1.svg"); //return PhysicalFile(file, "image/svg+xml"); }); //启用目录浏览 出于安全考虑默认关闭 //同时需要调用 Startup.ConfigureServices 中的services.AddDirectoryBrowser(); 方法来添加所需服务 app.UseDirectoryBrowser(new DirectoryBrowserOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")), RequestPath = "/MyImages" }); //UseFileServer 结合了 UseStaticFiles、UseDefaultFiles 和 UseDirectoryBrowser(可选)的功能 app.UseFileServer(); //以下代码提供静态文件和默认文件。 未启用目录浏览。 app.UseFileServer(enableDirectoryBrowsing: true); //通过启用目录浏览基于无参数重载进行构建: app.UseFileServer(new FileServerOptions { FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")), RequestPath = "/StaticFiles", EnableDirectoryBrowsing = true }); app.UseRouting(); // app.UseRequestLocalization(); // app.UseCors(); app.UseAuthentication(); app.UseAuthorization(); // app.UseSession(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); //Run 委托不会收到 next 参数。 第一个 Run 委托始终为终端,用于终止管道。 //Run 是一种约定。 某些中间件组件可能会公开在管道末尾运行的 Run[Middleware] 方法 //用 Use 将多个请求委托链接在一起。 next 参数表示管道中的下一个委托。 可通过不 调用 next 参数使管道短路。 app.Use(async(context, next) => { // Do work that doesn't write to the Response. await next.Invoke(); // Do logging or other work that doesn't write to the Response. }); app.Run(async context => { await context.Response.WriteAsync("Hello, World!"); }); }
public CarApiTests() { AutofacContainer.BeginLifetimeScope(); mockObject = new Mock <ICarDal>(); }