今天看到一篇漫画,[3年.NET开发应聘大厂惨遭淘汰,如何翻身打脸面试官?],好多问题,一下子还真的回答不了,这里对这些问题进行了整理,增加下脑容量,哈哈。俗话说不想当将军的士兵不是好士兵,不想当架构师的程序员,不是一个努力要进步的程序员,努力加油,不断学习。有人说架构师都是一批秃顶的人,程序员都是一群XX,其实现实是,架构师好多不是秃顶,不用担心自己成了架构师变成秃顶,那都是吓人的。程序员也是懂得浪漫的,要不然那么多浪漫的程序,那么多酷炫的技巧都是怎么实现的。
1.C#中的委托是什么?事件是不是一种委托?
委托是一种引用类型,表示对具有特定参数列表和返回类型的方法的引用。委托用于将方法作为参数传递给其他方法。事件就是通过委托调用的方法。
例如:
public class DelegateTest { public delegate int AddDelegate(int a, int b); //定义委托类型 public AddDelegate addDelegate; //定义委托 public event AddDelegate AddDelegateForEvent; //定义事件 public int Add(int a, int b) { Console.WriteLine($"a:{a},b:{b}"); return a + b; } //委托和事件的使用 public static void Test() { DelegateTest text = new DelegateTest(); text.addDelegate = text.Add; ; text.addDelegate(1, 2); text.AddDelegateForEvent += text.addDelegate; text.AddDelegateForEvent += text.addDelegate; text.AddDelegateForEvent(10, 20);//或者下面的使用 //AddDelegate d = text.AddDelegateForEvent; //d(10, 20); //结果 //a: 1,b: 2 //a: 10,b: 20 //a: 10,b: 20 } //Func和Action的使用 public static void Test2() { Func<int, int, int> add = (int a, int b) => { return a + b; }; Action<int, int> addVoid = (int a, int b) => { int c = a + b; }; } }
C#中委托这篇文章,对委托有更多的介绍。Fun和Action是微软封装的委托,一个有返回值,一个没有,C#高级功能(三)Action、Func,Tuple这篇文章介绍的比较详细。
2.聊聊.NET的管道和.NET Core的中间件
.NET的管道:在管道模型运行开始前,首先HTTP的请求被被传递到HttpRuntime类的一个实例中,然后这个实例对象检测请求并找到被接受的那个应用程序,接下来管道模型就使用HttpApplicationFactory对象来创建一个HttpApplication对象来处理这个请求(在此同时也将创建HttpContext,HttpRequest和HttpResponse),一个HttpApplication可以包含一系列HttpModule对象。
ASP.NET MVC请求生命周期
URL Routing Module →→ Matching Route Entry →→ Route Handle →→ Http Handle →→ Controller Factory →→ Controller →→ Action Invoker →→ Module Binders →→ Authentication Filter →→ Authorization Filter →→ Action Filter →→ Action Execution →→ Action Filter →→ Action Result
简单就是:Url →→ Route →→ Controller →→ Action →→ View
其他的ASP.NET生命周期的文章,ASP.NET生命周期 , WebForm页面运行周期--页面关系
.NET Core的中间件
中间件是一种装配到应用管道中以处理请求和响应的程序,使用Run、Map和Use扩展方法来配置请求委托。请求委托用于构建请求管道,处理每个HTTP请求。每个委托可以在下一个委托之前和之后执行操作。委托还可以决定不将请求传递给下一个委托,这称为请求管道的短路。短路通常是可取的,因为它避免了不必要的工作。
public class Startup { //此处省略部分代码,创建一个新的Core web项目,可以自行查看 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } }
Configure方法中的就是中间件,中间件组件的顺序定义了在请求上调用它们的顺序,以及响应的相反顺序,此排序对于安全性,性能和功能至关重要。
常用的中间件顺序
1. 异常/错误处理
2. HTTP 严格传输安全协议,HTTP协议介绍
3. HTTPS 重定向
4. 静态文件服务器
5. Cookie 策略实施
6. 身份验证
7. 会话
8.MVC
中间件例子:
public class LogMiddleware { private readonly RequestDelegate _next; public LogMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { Debug.WriteLine("程序运行 开始。"); await _next(context); Debug.WriteLine("程序运行 结束。"); } } public static class LogMiddlewareExtensions { public static IApplicationBuilder UseLog(this IAp