.NET 2.0 Web 服务 WSDL 错误元素被忽略?

发布于 2024-07-17 05:36:50 字数 726 浏览 1 评论 0原文

我有一个 Web 服务方法,我想在其中抛出一些自定义异常,例如 SomeException、SomeOtherException 等,然后 Web 服务将转换为客户端能够处理的 SOAP 错误。 在 Java 中,我可以在 WSDL 的 wsdl:operation 元素中包含 wsdl:fault 元素。 .NET 似乎没有考虑到这一点,并且没有办法将属性放在 WebMethod 上来显示可能发生的 SOAP 错误。

如果我创建一个具有 wsdl:fault 元素的 Java Web 服务并添加对 .NET 项目的 Web 引用,我会期望 wsdl:fault 元素会导致创建适当命名的异常,就像创建其他实体一样,但这似乎并非如此。

WSDL 中的 wsdl:fault 元素是否会被 .NET 完全忽略? 它们是 http://www.w3.org/TR/wsdl 所以这不是我所期望的行为。

如果是这种情况,可能的解决方法可能是返回一个结果对象,其中包含成功/失败布尔值和错误消息/枚举。 或者使用 SoapExceptions。 如果我选择使用 SoapExceptions,那么我会强调 Web 服务的用户来处理这些异常并正确反序列化它。 这两种方法似乎都不是处理此问题的好方法,并且会添加额外的问题和代码来解决此问题。

有什么建议吗?

I have a web service method where I would like to throw some custom exceptions e.g. SomeException, SomeOtherException etc which the web service would then turn into a SOAP fault which the client would be able to handle. In Java I can have wsdl:fault elements within the wsdl:operation element in the WSDL. It appears it .NET that this is not catered for and that there is no way of putting attributes on a WebMethod to show what SOAP faults may occur.

If I create a Java web service which has wsdl:fault elements and add a web reference to a .NET project I would have expected the wsdl:fault elements to cause appropriately named exceptions to be created just as the other entities are created, this however does not seem to be the case.

Is it the case that wsdl:fault elements in a WSDL are completly ignored by .NET? They are part of the WSDL specification defined at http://www.w3.org/TR/wsdl so this wasn't the behaviour I was expecting.

If this is the case possible work arounds might be returning a result object which contains a success/failure boolean value and an error message/enum. Or by using SoapExceptions. If I choose to use SoapExceptions I am then putting the emphesis on the user of my web service to handle these and deserialize it properly. Both of these don't seem a great way of handling this and add extra problems and code to workaround this.

Any advice?

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

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

发布评论

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

评论(3

灯下孤影 2024-07-24 05:36:50

既然您要求使用 .net 2.0,我想您知道这在 WCF 中是“固定的”,您可以在其中添加属性 [FaultContract(typeof(YourCustomException))]。

在 2.0 中完成此操作的“正常”方式是如您所说,添加带有 success-boolean、Result 和 Error 属性的响应消息。

您通常可以在 EntLib 中看到这是如何完成的。

Since you ask for .net 2.0 i guess you know that this is "fixed" in WCF where you can add the attribute [FaultContract(typeof(YourCustomException))].

The "normal" way this was done in 2.0 is as you says, add a Response message with a success-boolean, Result and a Error property.

You can typically see how this is done in EntLib.

眼泪都笑了 2024-07-24 05:36:50

ASMX Web 服务在客户端或服务器上都不支持 wsdl:fault 元素。 他们永远不会。

正如 ThorHalvor 所说,此错误修复称为“WCF”。

我已经成功地手写了一个包含 wsdl:fault 元素的 WSDL,然后通过 ASMX Web 服务返回这些错误,方法是将错误消息作为 SoapException 的 Detail 属性包含在内。 然后,Java 和 WCF 客户端正确地将其视为适当类型的异常。

ASMX web services did not support the wsdl:fault element, either on the client or the server. They never will.

As ThorHalvor has said, the bug fix for this is called "WCF".

I have successfully hand-written a WSDL that includes wsdl:fault elements, then returned those faults through an ASMX web service by including the fault message as the Detail property of a SoapException. Java and WCF clients then properly saw this as an exception of the appropriate kind.

晨曦慕雪 2024-07-24 05:36:50
[return: System.Xml.Serialization.XmlElementAttribute("ResultWS", typeof(ResultWS), Namespace = "http://...")]
[return: System.Xml.Serialization.XmlElementAttribute("ResultFaultWS", typeof(ResultFaultWS), Namespace = "http://...")]
public object SumTest_Operation([System.Xml.Serialization.XmlElementAttribute(Namespace = "http://...")] ParamWS param)
{            
    ResultWS result = null;
    try
    {
        result.Value = param.P1 + param.P2;              

    }
    catch (Exception)
    {
        ResultFaultWS resultFault = new ResultFaultWS();
        resultFault.Status = noOK;

        return resultFault;;
    }
    return result;
}    
[return: System.Xml.Serialization.XmlElementAttribute("ResultWS", typeof(ResultWS), Namespace = "http://...")]
[return: System.Xml.Serialization.XmlElementAttribute("ResultFaultWS", typeof(ResultFaultWS), Namespace = "http://...")]
public object SumTest_Operation([System.Xml.Serialization.XmlElementAttribute(Namespace = "http://...")] ParamWS param)
{            
    ResultWS result = null;
    try
    {
        result.Value = param.P1 + param.P2;              

    }
    catch (Exception)
    {
        ResultFaultWS resultFault = new ResultFaultWS();
        resultFault.Status = noOK;

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