|
Consuming the ASP .NET/C# Web Service Application: C++ Program Example
In this exercise we need ASP .NET/C# or ASP /NET VB .NET web service which hosted on the local IIS web server. Fortunately these web services already created in the previous exercises. In this exercise we are going to consume the methods exposed by the web service application previously created in C# (or VB .NET). The ASP .NET web service was hosted on the local IIS web server. Take note there is no C++/CLI ASP .NET web service application template in Visual Studio 2005/2008.
Creating the C++ CLR Console Application
Firstly create a new console application project.
You can use the following project and solution names if you want else change accordingly.
|
We are going to use the previously created C# ASP web service application and call it from C++ application. Select the project folder in the Solution Explorer > Right-click mouse > Select Add Web Reference menu.
In the Add Web Reference page, select the second link: Web services on the local machine.
Next, select the Service1 link.
You can test the exposed methods at this stage. Leave the web reference name to 'localhost' and click Add Reference button.
The added web service reference or normally called proxy class is visible in the Solution Explorer.
Next, edit the WebServiceClientCP.cpp file as given below. We just include the addition operation in this example and you can add other three numerical operations if you want.
// WebServiceClientCP.cpp : main project file.
#include "stdafx.h"
using namespace System; using namespace System::Net;
/// <summary> /// MyMathService gets a WebRequest and sets the default credentials /// and a custom header on the request. If you need control over the /// HTTP semantics of a Web service request it is much better to override /// the auto generated proxy class so that you don't lose the changes when /// the proxy gets re-generated. /// </summary> public ref class MyMathService:localhost::Service1 { // GetWebRequest is called before the SOAP request is sent protected: virtual WebRequest^ GetWebRequest(System::Uri^ url) override { // Create a request based on the supplied URI WebRequest^ request = WebRequest::Create(url); // Set default credentials request->Credentials = CredentialCache::DefaultCredentials; // Set a custom header value on the request request->Headers["custom-header"] = "custom-value"; return request; } };
[STAThread] int main(array<System::String ^> ^args) { float result; // Get x,y from the console, just a demo for addition operation Console::WriteLine("Please provide the x and y values to be added"); Console::Write("X:"); int x = int::Parse(Console::ReadLine()); Console::Write("Y:"); int y = int::Parse(Console::ReadLine()); // Instantiate the derived math service Console::WriteLine("Instantiating the derived math service..."); MyMathService^ mathService = gcnew MyMathService(); // Call the add method and print the result Console::WriteLine("Call the add method from the ASP web service and print the result..."); result = mathService->Add((float)x, (float)y); Console::WriteLine(x + " + " + y + " = " + result); return 0; } |
Then, build and run the project. Make sure in both steps there are no error. Key-in numerical values for X and Y which will be sent to the web service application and make sure the result returned by the web service and displayed by the console application is correct.