1

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.

Maksim Tyutmanov
  • 423
  • 1
  • 5
  • 12

1 Answers1

2

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);
}
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62
  • Thanks, this is what I wanted. I'm new to WCF and I didn't know about svcutil. However, it generated message contracts, not data contracts. "If the naming convention used to model messages is different from the WCF standard, code-generation tools will generate MessageContract classes for the request and response" - from [this](http://msdn.microsoft.com/en-us/magazine/ee335699.aspx) article. – Maksim Tyutmanov Jan 24 '12 at 11:23