【问题标题】:WCF FaultException <T> (fault contract) - .NET Core 2.2WCF FaultException <T>(故障协定)- .NET Core 2.2
【发布时间】:2019-04-11 11:47:39
【问题描述】:

.NET Core 2.2 不支持带有 FaultException 的故障合同吗?

我有一个这样的 WCF 服务参考 - 这个确切的代码未经测试,但 simular 在 .NET Standard 4.6.2 中运行良好。

服务器端:

[OperationContract]
[FaultContract(typeof(MyErrorType))]
[WebInvoke(
      Method = "GET",
      UriTemplate = "/GetData?foo={foo}",
      BodyStyle = WebMessageBodyStyle.Bare,
      RequestFormat = WebMessageFormat.Json,
      ResponseFormat = WebMessageFormat.Json)]
ExpectedReturnType GetData(int foo);

[DataContract]
[Serializable]
public class MyErrorType
{
    [DataMember]
    public int ErrorCode { get; set; }

    [DataMember]
    public string Description { get; set; }
}   

try {
   return GetData(123);
}
catch (Exception e)
{
    throw new FaultException<MyErrorType>(new MyErrorType() { ErrorCode  = 20,  Description = "Server side error message returning to client" });
}

客户端

try 
{
   _client.GetData(123);
}
catch (FaultException <MyErrorType> e)
{
  // NEVER ENDS UP HERE IN .NET Core 2.2
  // works fine in .NET Standard 4.6.2 
  Console.WriteLine(e.Details.ErrorCode);
  Console.WriteLine(e.Details.Description);
  throw e;
}
catch (Exception e)
{
  throw e;
}

【问题讨论】:

    标签: wcf .net-core-2.2


    【解决方案1】:

    我自己现在偶然发现了同样的问题。现在我做了这个似乎对我有用的解决方法。

    catch (FaultException<WarehouseFault> faultException) 
    {
        // FaultException is not supported in .Net Core as far as I know. 
        // So for now code is moved in general Exception handling below which seems to work.
        throw new Exception(".Net Core now seem to support FaultException!");
    }
    catch (Exception e) 
    {
        if (e.InnerException is FaultException<WarehouseFault> faultException) { 
            // Handle the fault exception here.
        }
    
        // Other exceptions
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      相关资源
      最近更新 更多