.NET Core 2.2中不支持与FaultException
我有这样的WCF服务参考-此确切的代码未经测试,但在.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;
}
答案 0 :(得分: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
}