关于ASP.NET WEB API(OWIN WEBAPI)的几个编码最佳实践总结
近期工作比较忙,确实没太多精力与时间写博文,博文写得少,但并不代表没有研究与总结,也不会停止我继续分享的节奏,最多有可能发博文间隔时间稍长一点。废话不多说,直接上干货,虽不是主流的ASP.NET CORE但ASP.NET WEB API仍然有很多地方在用【而且核心思路与.NET CORE其实都是一样的】,而且如下也适用于OWIN WEBAPI方式。
- 全局异常捕获处理几种方式
具体可参见: ASP.NET Web API 中的错误处理
自定义异常过滤器(ExceptionFilterAttribute) :当控制器方法引发不是带有 httpresponseexception异常的任何未经处理的异常时,将执行异常筛选器,仅处理与特定操作或控制器相关的子集未处理异常的最简单解决方案
/// <summary>
/// 统一全局异常过滤处理
/// author:zuowenjun
/// </summary>
public class GlobalExceptionFilterAttribute : ExceptionFilterAttribute
{
private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
var actionContext = actionExecutedContext.ActionContext;
string actionInfo = $"{actionContext.ActionDescriptor.ControllerDescriptor.ControllerName}.{actionContext.ActionDescriptor.ActionName}";
logger.Error(context.Exception, $"url:{actionContext.Request.RequestUri}({actionInfo}),request body:{actionContext.Request.Content.ReadAsStringAsync().Result},error:{context.Exception.Message}."); //为便于服务之间追踪分析,尝试把入参的TraceId继续传递给响应出参
string traceId = null;
if (actionContext.ActionArguments?.Count > 0)
{
var traceObj = actionContext.ActionArguments?.Values.Where(a => a is ITraceable).FirstOrDefault() as ITraceable;
traceId = traceObj?.TraceId;
} var apiResponse = new ApiResponse() { Code = 400, Msg = $"{actionInfo} Error:{context.Exception.Message}", TraceId = traceId }; actionExecutedContext.Response = actionExecutedContext.ActionContext.Request.CreateResponse(HttpStatusCode.OK, apiResponse, "application/json"); }
}
自定义异常处理器(ExceptionHandler):自定义 Web API 捕获的未处理异常的所有可能响应的解决方案
/// <summary>
/// 全局异常处理器
/// author:zuowenjun
/// </summary>
public class GlobalExceptionHandler : ExceptionHandler
{
private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); public override void Handle(ExceptionHandlerContext context)
{
var actionContext = context.ExceptionContext.ActionContext;
string actionInfo = $"{actionContext.ActionDescriptor.ControllerDescriptor.ControllerName}.{actionContext.ActionDescriptor.ActionName}";
logger.Error(context.Exception, $"url:{actionContext.Request.RequestUri}({actionInfo}),request body:{actionContext.Request.Content.ReadAsStringAsync().Result},error:{context.Exception.Message}."); //为便于服务之间追踪分析,尝试把入参的TraceId继续传递给响应出参
string traceId = null;
if (actionContext.ActionArguments?.Count > 0)
{
var traceObj = actionContext.ActionArguments?.Values.Where(a => a is ITraceable).FirstOrDefault() as ITraceable;
traceId = traceObj?.TraceId;
}
var apiResponse = new ApiResponse() { Code = 400, Msg = $"{actionInfo} Error:{context.Exception.Message}", TraceId = traceId };
context.Result = new ResponseMessageResult(context.ExceptionContext.Request.CreateResponse(HttpStatusCode.OK, apiResponse));
}
}
自定义委托处理程序中间件(DelegatingHandler):在请求响应管道中进行拦截判断,用于捕获并处理HttpError类异常。【如果想记录完整的异常信息,可以自定义多个继承自ExceptionLogger】
/// <summary>
/// 全局异常处理管道中间件(将除GlobalExceptionHandler未能捕获的HttpError异常进行额外封装统一返回)
/// author:zuowenjun
/// </summary>
public class GlobalExceptionDelegatingHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(respTask =>
{
HttpResponseMessage response = respTask.Result;
HttpError httpError;
if (response.TryGetContentValue(out httpError) && httpError != null)
{
var apiResponse = new ApiResponse<HttpError>()
{
Code = (int)response.StatusCode,
Msg = $"{response.RequestMessage.RequestUri} Error:{httpError.Message}",
Data = httpError,
TraceId = GetTraceId(response.RequestMessage.Content)
};
response.StatusCode = HttpStatusCode.OK;
response.Content = response.RequestMessage.CreateResponse(HttpStatusCode.OK, apiResponse).Content;
} return response; });
} private string GetTraceId(HttpContent httpContent)
{
string traceId = null;
if (httpContent is ObjectContent)
{
var contentValue = (httpContent as ObjectContent).Value;
if (contentValue != null && contentValue is ITraceable)
{
traceId = (contentValue as ITraceable).TraceId;
}
}
else
{
string contentStr = httpContent.ReadAsStringAsync().Result;
if (contentStr.IndexOf("traceId", StringComparison.OrdinalIgnoreCase) >= 0)
{
var traceObj = httpContent.ReadAsAsync<ITraceable>().Result;
traceId = traceObj.TraceId;
}
} return traceId;
}
}
Web Api Config入口方法(OWIN 是startup Configuration方法)注册上述相关的自定义的各类中间件:
【注:建议一般只需要同时注册GlobalExceptionDelegatingHandler、GlobalExceptionHandler即可】
config.MessageHandlers.Insert(0, new GlobalExceptionDelegatingHandler());
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
config.Filters.Add(new GlobalExceptionFilterAttribute());
如下是自定义的一个可追踪接口,用于需要API入参透传TraceId(Model类实现该接口即代表可传TraceId),我这里是为了便于外部接口传TraceId方便,才放到报文中,其实也可以放在请求头中
public interface ITraceable
{
string TraceId { get; set; }
}
ACTION方法入参验证的几种方式
在Model类对应的属性上增加添加相关验证特性
一般是指System.ComponentModel.DataAnnotations命名空间下的相关验证特性,如: PhoneAttribute、EmailAddressAttribute 、EnumDataTypeAttribute 、FileExtensionsAttribute 、MaxLengthAttribute 、MinLengthAttribute 、RangeAttribute 、RegularExpressionAttribute 、RequiredAttribute 、StringLengthAttribute 、UrlAttribute 、CompareAttribute 、CustomValidationAttribute【这个支持指定自定义类名及验证方法,采取约定验证方法签名,第一个参数为具体验证的对象类型(不限定,依实际情况指定),第二个则为ValidationContext类型】
让Model类实现IValidatableObject接口并实现Validate方法,同时需注册自定义的ValidatableObjectAdapter对象,以便实现对IValidatableObject接口的支持,这种方式的好处是可以实现集中验证(当然也可以使用上述方法1中的CustomValidationAttribute,并标注到要验证的Model类上,且指定当前Model类的某个符合验证方法即可)
public class CustomModelValidator : ModelValidator
{
public CustomModelValidator(IEnumerable<ModelValidatorProvider> modelValidatorProviders) : base(modelValidatorProviders)
{ } public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
if (metadata.IsComplexType && metadata.Model == null)
{
return new List<ModelValidationResult> { new ModelValidationResult { MemberName = metadata.GetDisplayName(), Message = "请求参数对象不能为空。" } };
} if (typeof(IValidatableObject).IsAssignableFrom(metadata.ModelType))
{
var validationResult = (metadata.Model as IValidatableObject).Validate(new ValidationContext(metadata.Model));
if (validationResult != null)
{
var modelValidationResults = new List<ModelValidationResult>();
foreach (var result in validationResult)
{
modelValidationResults.Add(new ModelValidationResult
{
MemberName = string.Join(",", result.MemberNames),
Message = result.ErrorMessage
});
} return modelValidationResults;
}
return null;
} return GetModelValidator(ValidatorProviders).Validate(metadata, container);
}
}
除了定义上述的CustomModelValidator以便支持对IValidatableObject的验证外,还必需注册加入到验证上下文集合中才能生效,仍然是在WEB API的config入口方法(OWIN 是startup Configuration方法),类似如下:
RegisterDefaultValidatableObjectAdapter(config); private void RegisterDefaultValidatableObjectAdapter(HttpConfiguration config)
{
IEnumerable<ModelValidatorProvider> modelValidatorProviders = config.Services.GetModelValidatorProviders(); DataAnnotationsModelValidatorProvider provider = (DataAnnotationsModelValidatorProvider)
modelValidatorProviders.Single(x => x is DataAnnotationsModelValidatorProvider); provider.RegisterDefaultValidatableObjectAdapter(typeof(CustomModelValidator));
}
出入参JSON格式及日期处理
大小写及强制仅支持JSON交互方式(以便符合目前通行的JSON规范,若不加则是大写)
config.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
日期格式化(如果不设置,则默认序列化后为全球通用时,不便于阅读)
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
自定义类型转换(通过自定义实现JsonConverter抽象类,可灵活定义序列化与反序化的行为),例如:
//在入口方法加入自定义的转换器类
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new BoolenJsonConverter()); public class BoolenJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(bool).IsAssignableFrom(objectType);
} public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Boolean)
{
return Convert.ToBoolean(reader.Value);
} throw new JsonSerializationException($"{reader.Path} value: {reader.Value} can not convert to Boolean");
} public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (value == null)
{
writer.WriteNull();
return;
}
if (value is bool)
{
writer.WriteValue(value.ToString().ToLower());
return;
} throw new JsonSerializationException($"Expected Boolean object value: {value}");
}
}
后面计划将分享关于OAuth2.0原生实现及借助相关框架实现(JAVA与C#语言),敬请期待!
温馨提示:我的分享不局限于某一种编程语言,更多的是分享经验、技巧及原理,让更多的人受益,少走弯路!
关于ASP.NET WEB API(OWIN WEBAPI)的几个编码最佳实践总结的更多相关文章
- 使用 OWIN Self-Host ASP.NET Web API 2
Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层.OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序而离 ...
- 使用 OWIN 作为 ASP.NET Web API 的宿主
使用 OWIN 作为 ASP.NET Web API 的宿主 ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动 设备)的 HTTP 服务. ASP.NET ...
- Use OWIN to Self-Host ASP.NET Web API 2
Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层.OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序 ...
- [转] JSON Web Token in ASP.NET Web API 2 using Owin
本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...
- JSON Web Token in ASP.NET Web API 2 using Owin
In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...
- 在ASP.NET Web API 2中使用Owin OAuth 刷新令牌(示例代码)
在上篇文章介绍了Web Api中使用令牌进行授权的后端实现方法,基于WebApi2和OWIN OAuth实现了获取access token,使用token访问需授权的资源信息.本文将介绍在Web Ap ...
- 在ASP.NET Web API 2中使用Owin基于Token令牌的身份验证
基于令牌的身份验证 基于令牌的身份验证主要区别于以前常用的常用的基于cookie的身份验证,基于cookie的身份验证在B/S架构中使用比较多,但是在Web Api中因其特殊性,基于cookie的身份 ...
- ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端通过CNBlogsAuthorizationServerProvider(Authorization ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- (转)【ASP.NET Web API】Authentication with OWIN
概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...
随机推荐
- postman+springboot一次上传多个文件
开发中到前端一次上传多个文件的需求如何实现,下面使用postman模拟前端的请求,后端使用srpingboot来实现 1.postman设置 2.Java代码 @RestController @Req ...
- 深度学习基础课:使用Adam算法
大家好~我开设了"深度学习基础班"的线上课程,带领同学从0开始学习全连接和卷积神经网络,进行数学推导,并且实现可以运行的Demo程序 线上课程资料: 本节课录像回放 加QQ群,获得 ...
- vue 使用print.js实现前端打印功能
https://blog.csdn.net/cccdf_jjj/article/details/99563682 插件vue-print-nb实现前端打印当前页面功能 https://blog.csd ...
- python之HtmlTestRunner(二)view无法打开问题解决
默认使用python之HtmlTestRunner会遇到测试报告中的view无法打开的情况 view打不开的情况解决 打开\Lib\site-packages\HtmlTestRunner\templ ...
- java进阶(5)--package与import
一.package 1.package的作用:为了方便程序的管理 2.package怎么使用:package+包名,只能出现在java代码的第一行 3.package命令规范:一般采用公司域名倒序方式 ...
- 如何使用单纯的`WebAssembly`
一般来说在.net core使用WebAssembly 都是Blazor ,但是Blazor渲染界面,.net core也提供单纯的WebAssembly这篇博客我将讲解如何使用单纯的WebAssem ...
- 在线视频点播网站(python实现)
本文将会对该项目进行一个简单的介绍,包括项目名称.项目背景.项目功能.技术栈等等. 项目名称 在线视频点播网站开发(python+django) 项目背景 学习完毕python和django之后,想找 ...
- SV 数据类型-3
联合数组 在内存中分配的空间可以是不连续的 联合数组方法 数组的方法 数组使用推荐 结构体 枚举类型 字符串变量类型String 操作符
- AMBA Bus介绍_01
AMBA总线概述 系统总线简介 AMBA 2.0 AHB - 高性能Bus APB - 外设Bus AHB ASB APB AHB 组成部分 APB组成部分 AMBA协议其他有关问题 DMA DMA ...
- 关于编写C时的调试--VS,VSCODE,DEV-C++
1.问题 VS最大的问题是不支持scanf函数,后面的调试部分我就没试了,主要用VS来写C++ VSCODE是服了他的C/C++插件,我死活卡在debug中scanf的输入部分,集成终端根本无法输入, ...