Writing custom Message Formatter for SOAP basicHttpBinding

Posted by Lijo on Programmers See other posts from Programmers or by Lijo
Published on 2012-09-11T11:32:01Z Indexed on 2012/09/11 15:49 UTC
Read the original article Hit count: 306

Filed under:
|
|

I have a WSDL published by our service development team. It is using SOAP and basicHttpBinding. I can add the service reference to the project using Add Service Reference option in Visual Studio.

I need to develop the WCF client. I need to use custom Message Formatter (for mapping between Messages and CLR types). Can you please show how to write the custom Message Formatter (in C# )for the following wsdl?

Note: I am planning to use custom Message Formatter due to an issue mentioned in http://stackoverflow.com/questions/12316884/header-namespace-mismatch-issue

WSDL

<definitions xmlns:import0="urn:thinktecture-com:demos:restaurantservice:data:v1" 
         xmlns:import2="urn:thinktecture-com:demos:restaurantservice:messages:v1" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns:import1="urn:thinktecture-com:demos:restaurantservice:headerdata:v1" 
         xmlns:tns="urn:thinktecture-com:demos:restaurantservice:wsdl:v1" 
         xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
         xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
         name="RestauarntService" 
         targetNamespace="urn:thinktecture-com:demos:restaurantservice:wsdl:v1" 
         xmlns="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
 <types>
<xsd:schema>
  <xsd:import schemaLocation="RestaurantData.xsd" namespace="urn:thinktecture-com:demos:restaurantservice:data:v1" />
  <xsd:import schemaLocation="RestaurantHeaderData.xsd" namespace="urn:thinktecture-com:demos:restaurantservice:headerdata:v1" />
  <xsd:import schemaLocation="RestaurantMessages.xsd" namespace="urn:thinktecture-com:demos:restaurantservice:messages:v1" />
</xsd:schema>
</types>
<message name="getRestaurantsIn">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import2:getRestaurants" />
</message>
<message name="getRestaurantsOut">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import2:getRestaurantsResponse" />
</message>
<message name="lijosCustomFaultMessage">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="fault" element="import2:customFault" />
</message>
<message name="userCredentialsIn">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import1:userCredentials" />
</message>
<message name="addRestaurantIn">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import2:addRestaurant" />
</message>
<message name="addRestaurantInHeader1">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import1:userCredentials" />
</message>
<message name="customFaultIn">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<part name="parameters" element="import2:customFault" />
</message>
<portType name="RestauarntServiceInterface">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<operation name="getRestaurants">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:getRestaurantsIn" />
  <output message="tns:getRestaurantsOut" />
  <fault name="lijosCustomFaultMessage" message="tns:lijosCustomFaultMessage" />
</operation>
<operation name="userCredentials">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:userCredentialsIn" />
</operation>
<operation name="addRestaurant">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:addRestaurantIn" />
</operation>
<operation name="customFault">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:customFaultIn" />
</operation>
</portType>
<binding name="BasicHttpBinding_RestauarntServiceInterface" type="tns:RestauarntServiceInterface">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getRestaurants">
  <soap:operation soapAction="urn:thinktecture-com:demos:restaurantservice:wsdl:v1:getRestaurantsIn" style="document" />
  <input>
    <soap:body use="literal" />
  </input>
  <output>
    <soap:body use="literal" />
  </output>
  <fault name="lijosCustomFaultMessage">
    <soap:fault use="literal" name="lijosCustomFaultMessage" namespace="" />
  </fault>
</operation>
<operation name="userCredentials">
  <soap:operation soapAction="urn:thinktecture-com:demos:restaurantservice:wsdl:v1:userCredentialsIn" style="document" />
  <input>
    <soap:body use="literal" />
  </input>
</operation>
<operation name="addRestaurant">
  <soap:operation soapAction="urn:thinktecture-com:demos:restaurantservice:wsdl:v1:addRestaurantIn" style="document" />
  <input>
    <soap:body use="literal" />
    <soap:header message="tns:addRestaurantInHeader1" part="parameters" use="literal"  />
  </input>
 </operation>
 <operation name="customFault">
  <soap:operation soapAction="urn:thinktecture-com:demos:restaurantservice:wsdl:v1:customFaultIn" style="document" />
  <input>
    <soap:body use="literal" />
  </input>
 </operation>
 </binding>
 <service name="RestauarntServicePort">
 <port name="RestauarntServicePort" binding="tns:BasicHttpBinding_RestauarntServiceInterface">
  <soap:address location="http://localhost/RestauarntService" />
 </port>
</service>
</definitions>??

RestaurantData.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="RestaurantData" targetNamespace="urn:thinktecture-com:demos:restaurantservice:data:v1"
elementFormDefault="qualified" xmlns="urn:thinktecture-com:demos:restaurantservice:data:v1"
xmlns:mstns="urn:thinktecture-com:demos:restaurantservice:data:v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="restaurantInfo">
    <xs:sequence>
        <xs:element name="restaurantID" type="xs:int" />
        <xs:element name="name" type="xs:string" />
        <xs:element name="address" type="xs:string" />
        <xs:element name="city" type="xs:string" />
        <xs:element name="state" type="xs:string" />
        <xs:element name="zip" type="xs:string" />
        <xs:element name="openFrom" type="xs:time" />
        <xs:element name="openTo" type="xs:time" />
    </xs:sequence>
  </xs:complexType>

 <xs:complexType name="restaurantsList">
    <xs:sequence>
        <xs:element name="restaurant" type="restaurantInfo" 
                    maxOccurs="unbounded" minOccurs="0" />
    </xs:sequence>
 </xs:complexType>

 <xs:complexType name="customFault">
 <xs:sequence>
  <xs:element name="errorCode" type="xs:string"/>
  <xs:element name="message" type="xs:string"/>
  <xs:element maxOccurs="unbounded" minOccurs="0" name="messages" 
              type="xs:string" />
  </xs:sequence>
  </xs:complexType>


  </xs:schema>

RestaurantHeaderData.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="RestaurantHeaderData" targetNamespace="urn:thinktecture-com:demos:restaurantservice:headerdata:v1"
elementFormDefault="qualified" xmlns="urn:thinktecture-com:demos:restaurantservice:headerdata:v1"
xmlns:mstns="urn:thinktecture-com:demos:restaurantservice:headerdata:v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="credentials">
    <xs:sequence>
        <xs:element name="username" type="xs:string" />
        <xs:element name="password" type="xs:string" />
    </xs:sequence>
</xs:complexType>

<xs:element name="userCredentials" type="credentials">
</xs:element>

</xs:schema>

? RestaurantMessages.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="RestaurantMessages" targetNamespace="urn:thinktecture-com:demos:restaurantservice:messages:v1"
elementFormDefault="qualified" xmlns="urn:thinktecture-com:demos:restaurantservice:messages:v1"
xmlns:mstns="urn:thinktecture-com:demos:restaurantservice:messages:v1" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:import="urn:thinktecture-com:demos:restaurantservice:data:v1">

<xs:import id="RestaurantData" schemaLocation="RestaurantData.xsd" 
           namespace="urn:thinktecture-com:demos:restaurantservice:data:v1">
</xs:import>

<xs:element name="getRestaurants">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="zip" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="getRestaurantsResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="restaurants" type="import:restaurantsList" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="addRestaurant">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="restaurant" type="import:restaurantInfo" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="customFault" type="import:customFault" />

</xs:schema>

?

© Programmers or respective owner

Related posts about c#

Related posts about .NET