Introduction

Creating your first web service is incredibly easy if you use C# or VB.NET (see my
previous article for details). Writing a WebService
using managed C++ in .NET is also extremely simple, but there are a couple of 'gotcha's that
can cause a few frustrating moments.

My first suggestion is to use the Visual Studio .NET Wizards to create your WebService (in fact it's
a great idea for all your apps when you are first starting out). This is especially
important if you are moving up through the various builds of the beta bits of .NET. What is perfectly
acceptable in one build may fail to compile in another build, and it may be difficult to work out
which piece of the puzzle you are missing.

Using the Wizards can get you a managed C++ WebService up and running in minutes, but things
can start to get a little weird as soon as you try something a little more risqué.

For this example I have created a service called MyCPPService by using the Wizard. Simply
select File | New Project and run through the wizard to create a C++ WebService.

A new namespace will be defined called CPPWebService, and within this namespace will be the
classes and structures that implement your webservice. For this example I have called the class
MyService. Other files that are created by the wizard include the .asmx
file that
acts as a proxy for your service; the config.web file for configuration settings, and the .disco file
for service discovery. Once you compile the class your assembly will be stored as
CPPWebService.dll
in the /bin directory.

I wanted to mimic the C# WebService created in my previous article,
but with a few minor changes to illustrate using value and reference types.
With this in mind I defined a Value Type structure ClientData and
a managed reference type ClientInfo within the namespace that would
both contain a
name and an ID (string and int values respectively).

__valuepublicstruct ClientData
{
String *Name;
int ID;
};

__gcpublicclass ClientInfo
{
String *Name;
int ID;
};

In order to return an array of objects a quick typedef is also declared

typedef ClientData ClientArray[];

In a similar fashion I defined my MyService class as a simple managed C++
class with three methods:

The important thing to notice about the function prototypes
is the [WebMethod] attribute - this informs the compiler that the method will be a method of a
web service, and that it should provide the appropriate support and plumbing. The method you attach this
attribute to must also be publicly accessible.

Note the use of the syntax i.ToString(). In .NET, value types such as int's and enums can
have methods associated with them. i.ToString() simply calls the Int32::ToString()
for the variable i.

One huge improvement of .NET beta 2 over beta 1 is that you no longer need to
mess around with the XmlIncludeAttribute class to inform the serializer
about your structure. A few bugs that either caused things to misbehave, or
worse - not run altogether - have also been fixed. Writing a WebService in MC++
is now just as easy in C++ as it is in C#, with the advantage that you can mix
and match native and managed code while retaining the raw power of C++.

Once you have the changes in place you can build the project then test the service by right clicking
on the CPPWebService.asmx in the Solution Explorer in Visual Studio and
choosing "View in Browser". The test page is shown below.

Clicking on one of the methods (say, GetClientsData) results in a
proxy page being presented which allows you to invoke the method directly from
your browser. The GetClientsData method takes a single intparameter
which you can enter in the edit box.

When invoked this returns the following:

Conclusion

Writing WebServices using Visual C++ with managed extensions is just as easy as writing them using C# or VB.NET,
as long as you remember a few simple things: use attributes, declare your classes as managed and make them
publicly accessible. Using the Visual Studio.NET wizards makes
writing and deploying these services a point and click affair, but even if you wish
to do it by hand then the steps involved are extremely simple.

Share

About the Author

Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

Comments and Discussions

I need to create web service in C/C++ on VxWorks platform, Can anybody let me know how to create the web services in c/C++ .how to host the web services in VxWorks. what all the basic things i need to do to host the service. so that client can make request and server respond to the request.

I'm trying to get some of these methods to run (they're C++ and therefore key, even though they're just exemplary) using ASP.NET Web Service wizard generated code in VSNET2K5 but mixing old syntax with new syntax doesn't cut the mustard. __value, __gc, etc have changed. For example, "int MyService::MyMethod" ... no problem. But this "ClientData MyService::GetClientData()" struct can't be an array now because of the mixed types!? How to declare this struct/class would be helpful. And what has happened to "typedef" is truly baffling. Some clarification there would also be very appreciated.

Using the C++.Net Std Edition (Version 7.0.9466) and MS .Net Framework 1.0 (Version 1.0.3705), when I 'Build' MyCPPService I get the message : 'MyCPPService - up-to-date.', but when I try to 'ReBuild MyCPPService' I get the message: 'MyCPPService error VCD0041: IIS must be installed on this machine in order for this program to function correctly.', and if I try to 'Start Without Debugging', a browser comes up with the address: 'http://localhost/MyCPPService/MyCPPService.asmx' and the page indicating 'The page cannot be displayed'.

Can anyone tell me what I am doing wrong? Do I need a different version of the compiler to accomplish this? Please help!

BTW, when I put my 'simple' Web Service up on my server, trying to access it via the Internet http://www.pliatech.com/MyHello.asmx I get server error ( I have also put the MyHello.dll and the MyHello.vsdisco in the same server directory).

I have a web service I have written. I was wondering if any of you know how to expose the web service interface in another program without using IIS. Instead of IIS hosting the web service, I would like my program to host the web service.