Category Archives: .NET

Unmanaged C++ client for WCF service.

Recently I have been evaluating different options and solutions for creating distributed network applications. Of course, this definition is too broad and there might be hundreds of answers and they all would be useful in different circumstances but I paid special attention to WCF.

As Microsoft released a Visual Studio 2008, I can hardly see any reasons why someone who is up to developing a distributed network application might choose anything different. New Studio even has a project template for WCF services, what makes network servers and clients development as easy as can be… But, of course, that would only work if no other platform except Windows is considered. Which is not uncommon.

So, what options are there if we need to build a client for existing WCF service that would work where .NET framework is not installed (because it is not available for that platform)? There is one way offered by Microsoft (using sproxy.exe) and some could use it. Other way involves writing “moniker” in .NET and then use it via COM. They both would work on windows platform only, moreover, I couldn’t make sproxy.exe to work with WCF service at all, so I had to look for the third option, which was gSOAP. And this worked.

Of course, you’ll need to download and install (and, probably, build before installing) gSoap, what, I hope, will not be a big problem. Then let’s assume we have a WCF service with following service contract running (this is just an example, I can hardly imagine a need in remote string concatenation service):

namespace SimpleServer
{
[ServiceContract]
public interface iStringService {
[OperationContract]
/**
*

* String concatenation *

* */ string Concat(string first, string second); } }

Writing, compiling and hosting the service is out of the scope of this article, however, I need to note that the only binding type I could make to work with gSoap with the service was Basic Http – keep it in mind when hosting your service.
Then is a step where a controversial feature of VS 2008 I have already written about (wcfsvchost.exe) really helps us – we need to get the service hosted and running before start using gSOAP. Run the project and have service hosted.

gSoap comes with two tools that make all stubs and bindings to programming language, which could be either C or C++, however, I was only interested in writing application in C++, which turned to be pretty straightforward.

At first, you’ll need to use wsdl2h.exe and the WCF service I mentioned above hosted and running. Assume that service entry point is http://localhost:8080/MyService/, and service WSDL is available on URL http://localhost:8080/MyService/metadata/?wsdl. This means that service endpoints’ configuration looks like the following:

name=”SimpleServer.Service1″>

binding=”basicHttpBinding” bindingConfiguration=”HttpBase” bindingNamespace=””
contract=”SimpleServer.iSimpleService”>

contract=”IMetadataExchange” />

;

Note – Visual Studio can generate metadata endpoint for you with some URL like “http://localhost:8978/Design_Time_Addresses…”, which can be used as well.

Execute the following:

wsdl2h.exe -o myService.h http://localhost:8080/MyService/metadata/?wsdl

It will create a file myService.h contains some basic bindings enough to write SOAP client in C. As I wanted to use C++, I had to move a little bit further and take a next step:

soapcpp2 -i -I ..\gsoap\import -x -C myService.h

Option -i tells compiler to derive SOAP proxies from soap struct. This is not necessary, but I noticed the code looks less messy this way.
Option -I specifies path to import directory in gSOAP distribution.
Option -C enables client-side code generation only
and -x is needed if you don’t need to have any sample XML message files.

This will generate proxies for all service’s binding types that were configured, but we are only interested in ones that start with “soapBasicHttpBinding_” (in my case, the full file names were soapBasicHttpBinding_USCOREiStringServiceProxy.h and soapBasicHttpBinding_USCOREiStringServiceProxy.cpp) because others do not usually work (I failed with net.tcp and did not try wsHttpbinding at all).

Now let’s use this proxy:

#include “soapBasicHttpBinding_USCOREiStringServiceProxy.h”
#include “soapBasicHttpBinding_USCOREiStringServiceProxy.h”
#include “BasicHttpBinding_USCOREiStringService.nsmap”
#include

int main(int argc, _TCHAR* argv[])
{
BasicHttpBinding_USCOREiStringServiceProxy MyProxy;
/* Uncomment two following strings and modify path to the
service according to your network configuration */
// static const char* const pszEndPoint=”http://localhost:8080/MyService/”;
// MyProxy.soap_endpoint=pszEndPoint;
std::string s1(“First string”);
std::string s2(“Second string”);
// Note this:
_ns1__Concat Param;
_ns1__ConcatResponse Response;
Param.first = &s1;
Param.second = &s2;
if (MyProxy.Concat(&Param,&Response)==SOAP_OK)
{
std::cout
<< "Service responded: " << (*Response.ConcatResult) ; } else { std::cout << "SOAP service call error" ; } } This still looks a little messy but gives an idea. If you look at BasicHttpBinding_USCOREiStringServiceProxy class implementation, you will see that each method of WCF service has corresponding method in that class, but signature is different. Method Concat has two parameters, first for input values and second for output, what explains what generated _ns1__Concat and _ns1__ConcatResponse structures are used for. That's it - this little example worked. Of course, you'll need to add generated files into your project as well as stdsoap2.cpp (which can be found in main directory of gSOAP) and pay attention to *.nsmap file. Of course, this does not cover all possible questions that may arise, but it gives a good starting point. From my point of view, gSOAP would be a good solution when there is a need to write a client for existing WCF service.

(Русский) WCF services development made easy in VS 2008?

Sorry, this entry is only available in Russian.

0 visitors online now
0 guests, 0 bots, 0 members