WCF-自定义响应消息

发布于 2024-12-28 04:04:45 字数 1031 浏览 0 评论 0原文

我收到了这样的 XSD 服务响应:

<xs:element name="AddEditResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

请注意,响应消息中唯一元素的名称是“响应”。

[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
    [OperationContract]
    [return:XmlElement("return")]
    bool AddEdit(MultipleElements elements);
}

我将 XmlElement 属性应用于 AddEdit 操作的返回值,但仍然收到以下 XSD:

<xs:element name="AddEditResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="AddEditResult" type="xs:boolean"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

AddEditResponse 元素内的元素名称保持不变,无论 [return:XmlElement] 属性中的名称如何。

为什么会发生这种情况?有没有办法在WCF中自定义数据交换格式的此类细节?

谢谢。

I was given such XSD for a service response:

<xs:element name="AddEditResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Notice that the name of the only element in response message is "response".

[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
    [OperationContract]
    [return:XmlElement("return")]
    bool AddEdit(MultipleElements elements);
}

I applied XmlElement attribute to the return value of AddEdit operation, but I'm still getting the following XSD:

<xs:element name="AddEditResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="1" maxOccurs="1" name="AddEditResult" type="xs:boolean"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

The name of the element inside the AddEditResponse element stays the same regardless of name in [return:XmlElement] attribute.

Why is this happening? Is there any way to customize such details of data exchange format in WCF?

Thanks.

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

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

发布评论

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

评论(1

我的奇迹 2025-01-04 04:04:45

我会通过让 svcutil 为我构建合同来解决这个问题。看看它是否有帮助...

使用您必须的架构至少通过一项操作生成 WSDL 文件,该文件与您的 AddEdit 方法相匹配。

完成后,使用类似的命令行运行 svcutil(在我的例子中,我使用的工具生成三个 WSDL 文件,其中一个引用 XSD 文件):

svcutil /mc AddEditSoapHttp.wsdl AddEditSoapBinding.wsdl AddEditInterface.wsdl WCF-WSDLFirst.xsd

结果应该是这样的:

Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating files...
....\AddEditSoapHttpService.cs
....\output.config

看一下生成的代码找到您问题的答案(也许还有更多)。

对于此 XSD(以请求/响应对为例):

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="AddEditRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="request" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="AddEditResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

我得到(我仅发布摘录):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://tempuri.org/ifx/addedit/interface/1/", ConfigurationName="AddEditPortType")]
public interface AddEditPortType
{

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ifx/addedit/bindings/1/AddEdit", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    AddEditResponse1 AddEdit(AddEditRequest1 request);
}

I would approach this by having svcutil build the contract for me. See if it helps...

Use the schema you have to generate a WSDL file with one operation at least, that matches your AddEdit method.

Once you have that, run the svcutil with a similar command line (in my case, the tool I am using generates three WSDL files one referencing the XSD file):

svcutil /mc AddEditSoapHttp.wsdl AddEditSoapBinding.wsdl AddEditInterface.wsdl WCF-WSDLFirst.xsd

The result should be something like this:

Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating files...
....\AddEditSoapHttpService.cs
....\output.config

Take a look at the generated code to find the answer to your question (and maybe more).

For this XSD (showing the request/response pair as an example):

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="AddEditRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="request" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="AddEditResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="response" type="xs:boolean"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I am getting (I am posting an excerpt only):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://tempuri.org/ifx/addedit/interface/1/", ConfigurationName="AddEditPortType")]
public interface AddEditPortType
{

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ifx/addedit/bindings/1/AddEdit", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute()]
    AddEditResponse1 AddEdit(AddEditRequest1 request);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文