c# – 在WCF中提供故障方法接收到没有详细信息的FaultException

c# – 在WCF中提供故障方法接收到没有详细信息的FaultException,第1张

概述我为我的服务实现了一个IErrorHandler,如果授权错误,则需要返回403而不是400. 这就是我的ProvideFault方法的样子: public void ProvideFault(Exception error, MessageVersion version, ref Message fault){ if (((FaultException)error).Code.SubC 我为我的服务实现了一个IErrorHandler,如果授权错误,则需要返回403而不是400.
这就是我的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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1243466.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存