
这就是我的ProvIDeFault方法的样子:
public voID ProvIDeFault(Exception error,MessageVersion version,ref Message fault){ if (((FaultException)error).Code.SubCode.name == "FailedAuthentication") { WebOperationContext.Current.OutgoingResponse.StatusCode = httpStatusCode.ForbIDden; WebOperationContext.Current.OutgoingResponse.StatusDescription = "StatusCode is forced to change in order to have the correct response on authentication."; }} 我想知道是否有任何方法可以为错误及其状态代码编写更好的检查,或者如何获得授权异常而不是FaultException女巫更通用?我觉得在FailedAuthentication字符串之后检查不太好,事实上它的授权不是认证…
解决方法 如果您使用的是C#6.0及更高版本,则可以使用 Exception Filters.示例为:public string Example() { try { /* Method Logic */ return string.Empty; } catch (System.Net.http.httpRequestException e) when (e.Message.Contains("400")) { return "unauthorized"; }} 在你的情况下:
public string Example() { try { /* Method Logic */ return string.Empty; } catch (System.Net.http.httpRequestException e) when (e.Message.Contains("400")) { ProvIDeFault(e,"",e.Message); }}public voID ProvIDeFault(System.Net.http.httpRequestException error,ref Message fault){ /* your logic according to your needs. */} 总结 以上是内存溢出为你收集整理的c# – 在WCF中提供故障方法接收到没有详细信息的FaultException全部内容,希望文章能够帮你解决c# – 在WCF中提供故障方法接收到没有详细信息的FaultException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)