Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

WCF channelfactory and DataContracts (complex data types) to consume gSoap web services

5.00/5 (1 vote)
2 Aug 2013CPOL3 min read 14.8K   210  
Datacontract channelfactory WCF way of consuming SOAP web services written in C, C++ using gSoap framework on Linux platform

Introduction

In this article, I am explaining the data contract and channelfactory WCF way of consuming a web service written in C/C++ using gSoap framework on Linux platform. Here service is implemented on Linux (C++ SOAP Server) platform using gSoap and client is WCF(C#-WCF SOAP Agent) based on Windows platform. We are exchanging user defined data type as the inter-operability issues are generally faced while exchanging them. Data contract abstractly describes the data to be exchanged between a service and a client thereby making it easy to inter-operate different technologies.     

Solving interoperability issues is one of the critical aspects of web services programming. This article shows how to make WCF (Windows Communication Foundation) and gSOAP interoperable. In WCF there are several ways of consuming web services like ServiceReference, MessageContracts/DataContracts using channelfactory etc. gSoap framework is used to implement and consume web services in C, C++ on various platforms.    

While I was working on a PoC for making WCF-gSoap interoperable. I went through a lot of searches and queries over google and other search engines. There I saw that many people are trying to make it interoperable and I could not find a solution over net. I think it can be a starting point for those who are trying to communicate WCF and gSoap.

Here onwards we are going to discuss the working WCF-gSoap Sample calc example attached with this article. In this example there is WCF client which sends SOAP request to perform calculations on integer data. gSoap Server (here listening at port 8080) serves the request and sends SOAP response back with complex data type data contract (class Result type defined below). To make gSOAP response
understandable by DataContractSerializer, You have to make certain changes on WCF client. Changes on WCF client and gSoap server both are required as follows:

Changes on WCF client side (SOAP Agent): 

The default namespace used by dataContract is a URI which is constructed based on the namespace the datacontract is defined in. But it is difficult to set such namespace on gSoap side as it is very much specific to Windows & WCF. Hence to deserialize  the response from the gSoap server, we set datacontract namespace to blank as follows:

C#
[DataContract(Namespace = "")                                      
public class Result
{
    [DataMember]
    public int resAdd { get; set; }
    [DataMember]
    public int resSub { get; set; }
    [DataMember]
    public int resMul { get; set; }
    [DataMember]
    public int resDiv { get; set; }
} 

Changes On gSOAP Service (SOAP Server): 

  1. The default namespace used by the WCF is “http://tempuri.org/”. Hence instead of modifying the service contract namespace on WCF Interface, we make following change in gSoap directives settings. It makes all methods on the gSOAP server coming under this web service lie under common WCF compliant namespace. The change is: 
  2. //gsoap ns service namespace:    http://tempuri.org/
  3. Also you need to change the service style to document as follows. By default it is rpc/encoded after your changes it becomes document/encoded. 
  4. //gsoap ns service style:    document
  5. You have to modify the function parameter name of web methods to webmethodnameResult. For example if the method name is Add, then the return parameter name should be AddResult. Look at the following declaration:
  6. C#
    int ns__PerformCalculations(int a, int b, struct Result &PerformCalculationsResult);

The equivalent representation of data contract on the gSoap server side is as follows:

C#
struct Result
{
    //Date members are arranged in sorted order as compared to WCF datacontract
    //because according to my observation data contract deserializer uses this technique.
    int resAdd;
    int resDiv;
    int resMul;
    int resSub;
};

Thus after making the changes on the WCF and gSoap side, compile the client and server. WCF service gets compiled using visual studio and compile gSoap service using ReadMe.txt provided with the attached zip file. After compilation run the server binary which will start listening at the port you specify and then run the WCF client console application. You may use HTTP debugger to see the traffic flowing back and forth. Thus you can make your WCF client talk to gSoap service.  I have attached working client and service with this article which can help you to understand the things practically and in detail.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)