因为在项目中,会有各种各样的领域异常或系统异常被抛出来,那么在cont新视野大学英语课件roller
里就需要进行完整的try-catch
捕获,并根据是否有异常抛出重新包装返回值。这是一项机械且繁琐的工作。有没有办法让框架自己去做这件事呢?
有的,解决方案的名称叫做全局异常处理,或者叫做如何让接口优雅地失败。
我们希望将异常处理和消息返回放到框架中进行统一处理,摆脱controller
层的try-catch
块。
一般而言用来实现全局异常处理的思路有两种,但是出发点都是通过.net web api
的管道中间件middleware pipeline
实现的。第一种方式是通过.net
内建的中间件来实现;第二种是完全自定义中间件实现。
我们会简单地介绍一下如何通过内建中间件实现,然后实际使用第二种方式来实现我们的代码,大家可以比较一下异同。
在api
项目中创建models
文件夹并创建errorrespon
类。
errorrespon.cs
using system.net;using system.text.json;namespace todolist.api.models;public class errorrespon{ public httpstatuscode statuscode { get; t; } = httpstatuscode.internalrvererror; public string message { get; t; } = "an unexpected error occurred."; public string tojsonstring() => jsonrializer.rialize(this);}
创建extensions文件夹并新建一个静态类exceptionmiddlewareextensions实现一个静态扩展方法:
exceptionmiddlewareextensions.cs
using system.net;using microsoft.aspnetcore.diagnostics;using todolist.api.models;namespace todolist.api.extensions;public static class exceptionmiddlewareextensions{ public static void uglobalexceptionhandler(this webapplication app) { app.uexceptionhandler(apperror => { apperror.run(async context => { context.respon.contenttype = "application/json"; var errorfeature = context.features.get<iexceptionhandlerfeature>(); if (errorfeature != null) { await context.respon.writeasync(new errorrespon { statuscode = (httpstatuscode)context.respon.statuscode, message = errorfeature.error.message }.tojsonstring()); } }); }); }}
在中间件配置的最开始配置好,注意中间件管道是有顺序的,把全局异常处理放到第一步(同时也是请求返回的最后一步)能确保它能拦截到所有可能发生的异常。即这个位置:
var app = builder.build();app.uglobalexceptionhandler();
就可以实现全局异常处理了。接下来我们看如何完全自定义一个全局异常处理的中间件,其实原理是完全一样的,只不过我更偏向自定义中间件的代码组织方式,更加简洁和一目了然。
与此同时,我们希望对返回值进行格式上的统一包装,于是定义了这样的返回类型:
apirespon.cs
using system.text.json;namespace todolist.api.models;public class apirespon<t>{ public t data { get; t; } public bool succeeded { get; t; } public string message { get; t; } public static apirespon<t> fail(string errormessage) => new() { succeeded = fal, message = errormessage }; public static apirespon<t> success(t data) => new() { succeeded = true, data = data }; public string tojsonstring() => jsonrializer.rialize(this);}
在api项目中新建middlewares文件夹并新建中间件globalexceptionmiddleware
globalexceptionmiddleware.cs
using system.net;using todolist.api.models;namespace todolist.api.middlewares;public class globalexceptionmiddleware{ private readonly requestdelegate _next; public globalexceptionmiddleware(requestdelegate next) { _next = next;血糖高要多吃什么 } public async task invokeasync(httpcontext context) { try { await _next(context); } catch (exception exception) { // 你可以在这里进行相关的日志记录 await handleexceptionasync(context, exception); } } private async task handleexceptionasync(httpcontext context, exception exception) { context.respon.contenttype = "application/json"; context.respon.statuscode = exception switch { applicationexception => (int)httpstatuscode.badrequest, keynotfoundexception => (int)httpstatuscode.notfound, _ => (int)httpstatusc印度说什么语言ode.internalrvererror }; var responmodel = apirespon<string>.fail(exception.me地理专业ssage); await context.respon.writeasync(responmodel.tojsonstring()); }}
这样我们的exceptionmiddlewareextensions就可以写成下面这样了:
exceptionmiddlewareextensions.cs
using todolist.api.middlewares;namespace todolist.api.extensions;public static class exceptionmiddlewareextensions{ public static webapplication uglobalexceptionhandler(this webapplication app) { app.umiddleware<globalexceptionmiddleware>(); return app; }}
首先我们需要在controller中包装我们的返回值,举一个createtodolist的例子,其他的类似修改:
todolistcontroller.cs
[httppost]public async task<apirespon<domain.entities.todolist>> create([frombody] createtodolistcommand command){ return apirespon<domain.entities.todolist>.success(await _mediator.nd(command));}
还记得我们在todolist的领域实体上有一个colour的属性吗,它是一个值对象,并且在赋值的过程中我们让它有机会抛出一个unsupportedcolourexception,我们就用这个领域异常来验证全局异常处理。
为了验证需要,我们可以对createtodolistcommand做一些修改,让它接受一个colour的字符串,相应修改如下:
createtodolistcommand.cs
public class createtodolistcommand : irequest<domain.entities.todolist>{ public string? title { get; t; } public string? colour { get; t; }}// 以下代码位于对应的handler中,省略其他...var entity = new domain.entities.todolist{ title = request.title, colour = colour.from(request.c化学元素olour ?? string.empty)};
启动api项目,我们试图以一个不支持的颜色来创建todolist:
请求
响应
顺便去看下正常返回的格式是否按我们预期的返回,下面是请求所有todolist集合的接口返回:
可以看到正常和异常的返回类型已经统一了。
其实实现全局异常处理还有一种方法是通过filter来做,具体方法可以参考这篇文章:filters in asp.net core,我们之所以不选择filter而使用middleware主要是基于简单、易懂,并且作为中间件管道的第一个个中间件加入,有效地覆盖包括中间件在内的所有组件处理过程。filter的位置是在路由中间件作用之后才被调用到。实际使用中,两种方式都有应用。
下一篇我们来实现put请求。
1.write custom asp.net core middleware
2.filters in asp.net core
到此这篇关于.net 6开发todolist应用之实现全局异常处理的文章就介绍到这了,更多相关.net 6 全局异常处理内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 07:36:44,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/8af152f896421c105d6d95186921ae03.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:.NET 6开发TodoList应用之实现全局异常处理.doc
本文 PDF 下载地址:.NET 6开发TodoList应用之实现全局异常处理.pdf
留言与评论(共有 0 条评论) |