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:
[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):
- 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:
//gsoap ns service namespace: http://tempuri.org/
- 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.
//gsoap ns service style: document
- 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:
int ns__PerformCalculations(int a, int b, struct Result &PerformCalculationsResult);
The equivalent representation of data contract on the gSoap server side is as follows:
struct Result
{
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.