WCF / WebService:可互操作的异常处理 [英] WCF/WebService: Interoperable exception handling
问题描述
我知道WCF会将异常转换为错误并将其作为SOAP消息发送回去,但是我想知道这是否真正可互操作。我想我很难解决这个可能的情况:
I understand that WCF will convert an exception into a fault and send it back as a SOAP message, but I was wondering if this is truly interoperable. I guess I'm having a tough time trying to figure out this possible scenario:
- 客户端(Java)调用WCF服务
(LoginService)。 - 服务器检查正确的授权,用户授权失败。
- 服务器抛出UnauthorizedAccessException。
- WCF以某种方式将其转换为Fault。 (*-另请参见)
- 客户端必须能够知道如何读取此故障。
- Client (Java) calls a WCF Service (LoginService).
- Server checks for proper authorization, user authorization fails.
- Server throws an UnauthorizedAccessException.
- WCF converts this into a Fault somehow. (* - See Below As Well)
- Client has to be able to know how to read this Fault.
我想我很难理解它如何仍可互操作,因为它期望Java知道如何转换.NET从UnauthorizedAccessException编码的SOAP Fault。
I guess I'm just having a tough time understanding how this could still be interoperable because it is expecting Java to know how to translate a SOAP Fault that .NET encodes from an UnauthorizedAccessException.
- 此外,.NET实际如何将异常转换为故障,作为故障代码,名称等内容输入。某些情况似乎是 du 就像故障名称可能是 UnauthorizedAccessException一样,但我宁可肯定而不是猜测。
推荐答案
没有自动转换。当您遇到未处理的异常时,WCF将返回错误(我忘记了哪个错误)。但是由于您没有声明该错误,因此即使返回错误,很多(即使不是大多数)客户端也会失败。
There is no "automatic conversion". WCF will return a fault (I forget which one) when you have an unhandled exception. But since you didn't declare that fault, many, if not most, clients will fail if you return it.
您应定义自己的错误并返回他们代替。考虑:
You are meant to define your own faults and to return them instead. Consider:
[DataContract]
public class MySpecialFault
{
public string MyMessage { get; set; }
}
[ServiceContract]
public interface IMyService
{
[FaultContract(typeof (MySpecialFault))]
[OperationContract]
void MyOperation();
}
public class MyService : IMyService
{
public void MyOperation()
{
try
{
// Do something interesting
}
catch (SomeExpectedException ex)
{
throw new FaultException<MySpecialFault>(
new MySpecialFault {MyMessage = String.Format("Sorry, but {0}", ex.Message)});
}
}
}
任何能够处理故障的客户端会处理这个。 WSDL将定义故障,并且他们将看到带有包含已发送的MySpecialFault实例的序列化版本的Detail元素的故障。他们将能够读取该实例的所有属性。
Any client capable of handling faults will deal with this. The WSDL will define the fault, and they will see a fault with the Detail element containing a serialized version of the MySpecialFault instance that was sent. They'll be able to read all the properties of that instance.
这篇关于WCF / WebService:可互操作的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!