|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article assumes you are familiar with declaring and using managed types and the .NET Garbage Collector. 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 A new namespace will be defined called 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 __value public struct ClientData { String *Name; int ID; }; __gc public class 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
// CPPWebService.h #pragma once #using "System.EnterpriseServices.dll" namespace CPPWebService { __value public struct ClientData { String *Name; int ID; }; __gc public class ClientInfo { String *Name; int ID; }; typedef ClientData ClientArray[]; __gc class MyService { public: [WebMethod] int MyMethod(); [WebMethod] ClientData GetClientData(); [WebMethod] ClientArray GetClientsData(int Number); }; } The important thing to notice about the function prototypes
is the The implementation (.cpp) file is as follows. #include "stdafx.h" #using <mscorlib.dll> #using "System.Web.dll" #using "System.Web.Services.dll" using namespace System; using namespace System::Web; using namespace System::Web::Services; #include "CPPWebService.h" namespace CPPWebService { int MyService::MyMethod() { return 42; } ClientData MyService::GetClientData() { ClientData data; data.Name = new String("Client Name"); data.ID = 1; return data; } ClientArray MyService::GetClientsData(int Number) { // simple sanity checks if (Number < 0 || Number > 10) return 0; ClientArray data = new ClientData __gc[Number]; if (Number > 0 && Number <= 10) { for (int i = 0; i < Number; i++) { data[i].Name = new String("Client "); data[i].Name->Concat(i.ToString()); data[i].ID = i; } } return data; } }; Note the use of the syntax One huge improvement of .NET beta 2 over beta 1 is that you no longer need to
mess around with the 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, When invoked this returns the following:
ConclusionWriting 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. HistoryOct 18 - updated for .NET beta 2
|
||||||||||||||||||||||