Patterns for application development with ASP.NET Core
此文章翻译自 NDC { London } 16-20 January 2017 上, Damian Edwards和David Fowler的演讲,如果翻译不周,请大家指出错误。
Logging
生产环境总是配置一个Logger(比如: Serilog, Application Insights)
日志作为诊断应用程序问题的入口
不需要重启应用程序,就能改变日志的级别
在开发环境应该记录尽可能多的日志,但是生产环境出于性能考虑,应该只记录Warning以上的日志
如果不想显示太多的信息,可以选择特定的Category
比如只想调试SQL语句,可以只记录Category为Microsoft.EntityFrameworkCore.*的日志。
ASP.NET按照如下方式记录日志:
ANCM(IIS): 将不能启动进程的错误记录到EventLog
ANCM是指ASP.NET Core Module。按照Damian的说法,当IIS进程无法启动的时候是很崩溃的,此时可以从Windows Event Log中查看具体的错误。
ANCM also uses regular IIS failed request tracing
其他未处理的异常将由为logger providers提供的logger记录
Configuration & Options
随着配置文件的改变而自动重新加载配置
当配置内容的值更改时,使用IOptionsSnapshot重新加载IOptions对象
IOptionsSnapshot是ASP.NET Core 1.1提供的新接口,这个接口在配置文件改变时会重新绑定配置对象的值,这样可以在应用程序运行时动态修改配置内容。
使用User Secrets, 环境变量,Azure KeyVault等方式存储和环境有关的配置内容
比如每个环境的连接字符串,启动的端口不一致,并且这些配置是很少发生改变的,可以配置在环境变量中。
应用程序部署在IIS时,可以通过web.config配置环境变量
File Upload
如果上传的文件需要提供给浏览器访问,将它们放在web root下
最简单的处理文件上传的方法,是使用MVC里的IFormFile或者IFormFileCollection
这2个接口可以直接定义为Action的参数,也可以定义在Model里。
和传统的ASP.NET相比,少了Request.Files。
也可以使用低级别的API,比如HttpRequest.ReadFormAsync和HttpRequest.Form
阻止用户上传任何可以在服务器执行的文件
比较容易疏忽的是.cshtml文件。ASP.NET Core 2.0提供了Razor Page,那么.cshtml文件很可能被制作成一个可以直接访问的Razor Page上传上来。
大文件会提供buffer,防止服务器内存暴增
在保存文件之前,检查客户端传递的文件路径
保证文件上传功能的安全性一直是个斗智斗勇的过程,David演示了一个简单的Hack场景。如果直接使用如下的方式保存文件:
var path = Path.Combine(env.ContentRoot, file.FileName);
当客户端传递来的FileName是"\Views\Home\Index.cshtml"时,你的网站就杯具了。
处理很大的multipart uploads时,不要使用model binding。手动的调用HttpRequest.GetMultipartBoundary()方法。
这个方法仅在ASP.NET CORE 1.1.0提供
IIS的限制仍然生效
这里应该说的是maxRequestSize之类的限制
Dependency Injection
保持你的DI工厂快速响应,不要在构造服务时使用异步方法
如下代码会导致死锁
class Program
{
static void Main(string[] args)
{
DeadlockWithFactory(); Console.WriteLine("hello");
} private static void DeadlockWithFactory()
{
var services = new ServiceCollection();
services.AddSingleton(s =>
{
var b = GetBAsync(s).Result; return new A(b);
}); services.AddSingleton<B>(); var serviceProvider = services.BuildServiceProvider();
var a = serviceProvider.GetService<A>(); //1. 此处lock了Container
} private static async Task<B> GetBAsync(IServiceProvider s)
{
await Task.Delay(1000); //2. 此处由于使用了await关键字,离开了主线程,进入了后台线程 return s.GetRequiredService<B>(); //3. 此处后台线程尝试lock Container,导致死锁
}
} public class A
{
public A(B b)
{ }
} public class B
{ }
避免手动的调用GetService
这里的大概意思是,由于手动调用GetService依赖了IServiceProvider,会使得单元测试变复杂。
有些场景下,我们不需要在构造函数注入服务,而是想在执行到特定方法的时候才注入。Controller层面有个FromServices特性可以标记在Action的参数上。如果是下层代码,则可以自己定义一个XXXProvider,提供一个ResolveXXX方法,那么只需要在构造函数注入XXXProvider即可。这种方式适用于构造数据库连接,打开文件等。
实现了IDisposable接口的服务会由Container控制调用它们的Dispose方法,如果在顶层Container中构造出该服务的实例,会导致内存泄漏。
Container构造的服务,会在Container释放的时候自动释放。如果在ApplicationServices上构造出的实例,在应用停止后才会释放。
Turn on scope validation to make sure you don't have scoped services capturing singletons.
想像一下这个场景
var services = new ServiceCollection();
services.AddSingleton<A>();
services.AddScoped<B>(); var sp = services.BuildServiceProvider();
var a = sp.GetRequiredService<A>();
如果A依赖了B,那么本应该是Scoped的B类型被捕获成了单例。
在ASP.NET Core 1.1中, BuildServiceProvider方法提供了一个新的参数 validateScopes,上面的代码可以修改成
var sp = services.BuildServiceProvider(validateScopes: true);
var a = sp.GetRequiredService<A>(); //如果A依赖B,那么此处会抛出System.InvalidOperationException: Cannot consume scoped service 'B' from singleton 'A'.
优化依赖注入
构造函数注入
public class A(B b) { }
public class B(C c) { }
public class C() { }
等同于
new A(new B(new C()));
ServiceProvider注入
public class A(IServiceProvider sp) { _b = sp.GetService<B>(); }
public class B(IServiceProvider sp) { _c = sp.GetService<C>(); }
public class C() { }
等同于
new C();
new B(GetService<C>());
new A(GetService<B>());
MVC
Protect against mass assignment with separate input/output models
在现有MVC模式中,我们一般定义一个ViewModel来展示整个页面需要显示的内容,再定义一些Model存储客户端传递的参数。我们要尽量避免这个过程中产生的重复编码。
public class AdminViewModel
{
[Display(Name = "Live Show Embed URL", Description = "URL for embedding the live show")]
[DataType(DataType.Url)]
public string LiveShowEmbedUrl { get; set; } [Display(Name = "Live Show HTML", Description = "HTML content for the live show")]
[DataType(DataType.MultilineText)]
public string LiveShowHtml { get; set; } [Display(Name = "Next Show Date/time", Description = "Exact date and time of the next live show in Pacific Time")]
[DateAfterNow(TimeZoneId = "Pacific Standard Time")]
public DateTime? NextShowDatePst { get; set; } [Display(Name = "Standby Message", Description = "Message to show on home page during show standby")]
public string AdminMessage { get; set; } public string NextShowDateSuggestionPstAM { get; set; } public string NextShowDateSuggestionPstPM { get; set; } public string SuccessMessage { get; set; } public bool ShowSuccessMessage => !string.IsNullOrEmpty(SuccessMessage); public AppSettings AppSettings { get; set; } public string EnvironmentName { get; set; }
} [ModelMetadataType(typeof(AdminViewModel))]
public class AdminInputModel
{
public string LiveShowEmbedUrl { get; set; } public string LiveShowHtml { get; set; } public DateTime? NextShowDatePst { get; set; } public string AdminMessage { get; set; }
}
在这个例子中,在Get请求时需要返回一个包含了所有展示内容的AdminViewModel,在Post请求时只需要客户端传递部分参数,因此可以定义一个AdminInputModel,同时指定ModelMetadataType(typeof(AdminViewModel)),这样验证的Attribute可以从AdminViewModel中自动复制过来,避免重复编码。
同时,推荐大家采用类似AutoMapper之类的第三方库解决对象复制、克隆过程中产生的重复编码。
在上述代码中,我更倾向于以如下的方式定义Model
public class AdminViewModel
{
public AdminInputModel Input { get; set; } public string NextShowDateSuggestionPstAM { get; set; } public string NextShowDateSuggestionPstPM { get; set; } public string SuccessMessage { get; set; } public bool ShowSuccessMessage => !string.IsNullOrEmpty(SuccessMessage); public AppSettings AppSettings { get; set; } public string EnvironmentName { get; set; }
} [ModelMetadataType(typeof(AdminViewModel))]
public class AdminInputModel
{
[Display(Name = "Live Show Embed URL", Description = "URL for embedding the live show")]
[DataType(DataType.Url)]
public string LiveShowEmbedUrl { get; set; } [Display(Name = "Live Show HTML", Description = "HTML content for the live show")]
[DataType(DataType.MultilineText)]
public string LiveShowHtml { get; set; } [Display(Name = "Next Show Date/time", Description = "Exact date and time of the next live show in Pacific Time")]
[DateAfterNow(TimeZoneId = "Pacific Standard Time")]
public DateTime? NextShowDatePst { get; set; } [Display(Name = "Standby Message", Description = "Message to show on home page during show standby")]
public string AdminMessage { get; set; }
}
TagHelpers
- 正确的处理属性值
- 属性中带有文件路径时,确保在执行前验证文件是否存在
在Post-Redirect-Get场景中使用TempData
当你在一个Post的请求里完成操作,并且需要重定向到另外一个页面展示操作结果时,可以通过URL传参,Cookies, Sessions等技术来实现,也可以使用TempData来实现。
ASP.NET Core的TempData是基于Cookies的。
Testing your pipeline
使用TestHost在内存中测试整个管道,而不用访问网络
[Fact]
public async Task VerifyResponse()
{
var builder = new WebHostBuilder()
.UseStartup<Startup>(); var server = new TestServer(builder);
var client = server.CreateClient(); var response = await client.GetAsync("http://something"); Assert.Equal("Hello World", await response.Content.ReadAsStringAsync());
}
单元测试中也可以使用enviroments来对不同的环境进行测试
[Fact]
public async Task VerifyTestEnvironmentResponse()
{
var builder = new WebHostBuilder()
.UseEnvironment("Test")
.UseStartup<StartupWithEnvironment>(); var server = new TestServer(builder);
var client = server.CreateClient(); var response = await client.GetAsync("http://something"); Assert.Equal("Test", await response.Content.ReadAsStringAsync());
}
Per request state
HttpContext.Items
和以往一样,可以将与会话相关的数据存储在HttpContext.Items
HttpContext.Get/SetFeature
通过调用HttpContext.Features.Set方法添加自定义的行为
Scoped services
每个会话开始会构造一个Scope,会话结束会释放该Scope。因此和会话相关的服务最好注册为Scoped。
在Request Context以外要访问HttpContext时,可以使用IHttpContextAcessor接口。
public class A
{
private HttpContext HttpContext { get; set; } public A(IHttpContextAcessor httpContextAcessor)
{
this.HttpContext = httpContextAcessor.HttpContext;
}
}
备注
Patterns for application development with ASP.NET Core的更多相关文章
- Prerender Application Level Middleware - ASP.NET Core Middleware
In the previous post Use Prerender to improve AngularJS SEO, I have explained different solutions at ...
- [转]Session and application state in ASP.NET Core
本文转自:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state By Rick Anderson and Steve ...
- 使用Azure Application Insignhts监控ASP.NET Core应用程序
Application Insignhts是微软开发的一套监控程序.他可以对线上的应用程序进行全方位的监控,比如监控每秒的请求数,失败的请求,追踪异常,对每个请求进行监控,从http的耗时,到SQL查 ...
- Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程
原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...
- [转]ASP.NET Core 1 Deploy to IIS
本文转自: http://webmodelling.com/webbits/aspnet/aspnet-deploy-iis.aspx 15 Sep 2016. This tutorial will ...
- 翻译 - ASP.NET Core 基本知识 - 配置(Configuration)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-5.0 ASP ...
- ASP.NET Core 1.0 入门——了解一个空项目
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- ASP.NET Core 2 学习笔记(二)生命周期
要了解程序的运行原理,就要先知道程序的进入点及生命周期.以往ASP.NET MVC的启动方式,是继承 HttpApplication 作为网站开始的进入点,而ASP.NET Core 改变了网站的启动 ...
- ASP.NET Core 2 学习笔记(十一)Cookies & Session
基本上HTTP是没有记录状态的协定,但可以通过Cookies将Request来源区分出来,并将部分数据暂存于Cookies及Session,是写网站常用的用户数据暂存方式.本篇将介绍如何在ASP.NE ...
随机推荐
- npm学习总结
1.npm run [scripts name]的作用及意义: npm 局部安装的工具包不能像全局安装那样直接执行命令行,但可写成命令行执行语句,通过npm run来运行,该命令可将node_modu ...
- bootstrap禁用点击空白处关闭模态框
原博主地址:http://www.cnblogs.com/godlovelian/p/4530342.html
- 3297: [USACO2011 Open]forgot
3297: [USACO2011 Open]forgot Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 69 Solved: 51[Submit][ ...
- Lucene实战之初体验
前言 最早做非结构化数据搜索时用的还是lucene.net,一直说在学习java的同时把lucene这块搞一搞,这拖了2年多了,终于开始搞这块了. 开发环境 idea2016.lucene6.0.jd ...
- 【树莓派】iptables相关配置
关于iptables的配置,参见官方资料:http://wiki.ubuntu.org.cn/IptablesHowTo 最好. 进入iptables # sudo iptables -L 列出目前的 ...
- Android开发之AChartEngine的使用
下面附上代码加注解 package com.example.com.my_achartnegine; import android.content.Context; import android.gr ...
- 豆瓣电影Top250基本信息抓取
豆瓣电影Top250基本信息抓取 最近想看电影,但是想看一些有营养的,所以就去豆瓣上看电影评分,但是豆瓣上的评分没有排序,所以就用python把网站内容爬下来了,然后按评分做了排序.具体代码参见git ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- ECMAScript版本号总结
最近想要研究下ES6,关于这个标准的发展历史.ES5. ES6.ES2015等等名称的定义都不怎么明确,查了很多资料,去除了程序员不关心的信息,下面是对ECMAScript规范发展历史及名词定义的 ...
- 关于RestfulAPI与SpringMVC之间的传值
##默认读者已经搭建好的SpringMVC的环境,并且对SpringMVC有着一定的了解,并且已经对RestfulAPI有了一定的了解 1,首先将WEB-INF下的web.xml里面的url-part ...