Maksim Tyutmanov
Maksim Tyutmanov

Reputation: 423

WCF - customize response message

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.

Upvotes: 1

Views: 888

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

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

Upvotes: 2

Related Questions