返回自定义 XML 的 Web 服务

发布于 2024-09-06 22:56:22 字数 1684 浏览 2 评论 0原文

我正在处理一个遗留的 Web 服务,并且在尝试调整方法对先前定义的 XML 的响应时遇到了麻烦。遗憾的是,更改 WSDL 是不可能的,并且使问题进一步复杂化的是,它与 WSDL.exe 工具不兼容。

哦!如此想要的 XML:

<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <Response xmlns="my/Namespace">Success</Response>
    </soap:Body>
</soap:Envelope>

通过实验 - 因为我不熟悉 .NET - 我已经达到了下面的解决方案,如果两种方法使用不同的 XML 标记,该解决方案将起作用。不幸的是,当 ResponseElementName 设置为相同的值时,它会“破坏”Web 服务并出现以下错误:

命名空间“my/Namespace”中的 XML 元素“Response”引用了一个方法,并且一种类型。使用 WebMethodAttribute 更改方法的消息名称或使用 XmlRootAttribute 更改类型的根元素。

那么,是否有另一种方法可以在不更改整个系统的情况下实现 XML 涅槃呢?

先感谢您!

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;

[WebService(Name = "WSOcean",
    Namespace = "my/Namespace"
)]
public class WSOcean : System.Web.Services.WebService
{
    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method1",
        RequestNamespace = "my/Method1/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method1(int MessageType)
    {
        return "Example message 1";
    }

    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method2",
        RequestNamespace = "my/Method2/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method2(bool MessageStatus)
    {
        return "Random message 2";
    }
}

I'm dealing with a legacy web service and I'm running into trouble trying to adjust the methods' responses to previously defined XMLs. Sadly, changing the WSDL is out of question and to further complicate the issue, it's incompatible with WSDL.exe tool.

The oh!-so-wanted XML:

<soap:Envelope 
xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <Response xmlns="my/Namespace">Success</Response>
    </soap:Body>
</soap:Envelope>

Through experimentation - since I'm not fluent in .NET - I've reached the solution below, which would work if both methods used different XML tags. Unfortunately, when the ResponseElementName is set to the same value it "breaks" the web service with the following error:

The XML element 'Response' from namespace 'my/Namespace' references a method and a type. Change the method's message name using WebMethodAttribute or change the type's root element using the XmlRootAttribute.

So, is there another way to achieve the XML nirvana without changing the whole system?

Thank you in advance!

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;

[WebService(Name = "WSOcean",
    Namespace = "my/Namespace"
)]
public class WSOcean : System.Web.Services.WebService
{
    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method1",
        RequestNamespace = "my/Method1/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method1(int MessageType)
    {
        return "Example message 1";
    }

    [WebMethod]
    [SoapDocumentMethod(
        Action = "Method2",
        RequestNamespace = "my/Method2/Namespace",
        ResponseElementName = "Response"
    )]
    [return: XmlText(DataType = "string")]
    public string Method2(bool MessageStatus)
    {
        return "Random message 2";
    }
}

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

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

发布评论

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

评论(1

っ左 2024-09-13 22:56:22

首先,您应该使用 WCF 进行任何新的 Web 服务开发。

其次,尝试返回 XmlElement 类型而不是字符串。尝试准确构建您想要返回的 XML,然后返回它。

First of all, you should be using WCF for any new web service development.

Secondly, try returning the XmlElement type instead of string. Try building exactly the XML you want to return, then returning it.

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