WCF/WebService:可互操作的异常处理

发布于 2024-10-11 04:08:00 字数 483 浏览 12 评论 0原文

我知道 WCF 会将异常转换为错误并将其作为 SOAP 消息发送回来,但我想知道这是否真正具有互操作性。我想我很难弄清楚这种可能的情况:

  1. 客户端(Java)调用 WCF 服务 (登录服务)。
  2. 服务器检查授权是否正确,用户授权失败。
  3. 服务器抛出 UnauthorizedAccessException。
  4. WCF 以某种方式将其转换为故障。 (* - 也见下文)
  5. 客户必须能够知道如何读取此故障。

我想我只是很难理解这如何仍然是可互操作的,因为它期望 Java 知道如何转换 .NET 从 UnauthorizedAccessException 编码的 SOAP 错误。

  • 另外,.NET 实际上如何将异常转换为错误,错误代码、名称等内容是什么。有些内容似乎是“duh”,例如错误名称可能是“UnauthorizedAccessException”,但我'我宁愿确切地知道而不是猜测。

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:

  1. Client (Java) calls a WCF Service
    (LoginService).
  2. Server checks for proper authorization, user authorization fails.
  3. Server throws an UnauthorizedAccessException.
  4. WCF converts this into a Fault somehow. (* - See Below As Well)
  5. Client has to be able to know how to read this 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.

  • Also, how does .NET actually convert the exception to a fault, what goes in as the fault code, name, etc. Some of the things seem to be "duh"s like perhaps the Fault Name is "UnauthorizedAccessException", but I'd rather know for sure than guess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未蓝澄海的烟 2024-10-18 04:08:00

没有“自动转换”。当你有一个未处理的异常时,WCF 将返回一个错误(我忘记了是哪一个)。但由于您没有声明该错误,因此如果您返回该错误,许多(如果不是大多数)客户端都会失败。

你应该定义自己的错误并归还它们。考虑一下:

[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 元素的故障。他们将能够读取该实例的所有属性。

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)});
        }
    }
}

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.

注定孤独终老 2024-10-18 04:08:00

自 v1.1 起,故障就成为 SOAP 规范的一部分。 SOAP 规范中对它们进行了解释。

由实现(WCF、Java 等)来确保根据规范处理故障。

由于 WCF 根据 SOAP 规范将FaultException 转换为Fault,因此从WCF 抛出的FaultException 是可互操作的。

Faults have been part of the SOAP specification since v1.1. They are explained in the SOAP Specification.

It is up to implementations (WCF, Java etc) to ensure that Faults are handled according to the specification.

Since WCF converts FaultExceptions to Faults according to the SOAP specification, FaultExceptions thrown from WCF are interoperable.

煞人兵器 2024-10-18 04:08:00

SOAP 错误是可互操作的,但 .Net 异常类不适合在 SOAP 错误中使用。相反,定义您自己的DataContract 类(例如AccessFault),然后在FaultContract 中使用它。
请参阅 http://msdn.microsoft.com/en-us/library/ms733841。 。

每当在服务边界抛出 UnauthorizedAccessException 时,将其转换为FaultException
这可以通过多种方式完成,例如使用 Microsoft Enterprise Library 异常处理块或实现 IErrorHandler 接口。

SOAP faults are interoperable but .Net exception classes are not good to be used in SOAP faults. Instead define your own DataContract class (e.g. AccessFault) and then use it in a FaultContract.
see http://msdn.microsoft.com/en-us/library/ms733841.aspx

Whenever there is a UnauthorizedAccessException thrown at service boundary convert it to FaultException.
This can be done in several ways like using Microsoft Enterprise Library Exception Handling Block or implementing the IErrorHandler interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文