< ASP .NET & VB .NET Web Service Program Example | Main | C#/ASP .NET Asynchronous Web Service Program Example >

 


 

 

Chapter 11 Part 5:

XML Web Services and the Network

 

 

What do we have in this chapter 11 Part 5?

  1. Consuming the ASP .NET/C# Web Service Application: C++ Program Example

  2. Creating the C++ CLR Console Application

  3. Adding the ASP .NET/C# Web Service Reference

  4. Build, Run and Test the Whole Project

 

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.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: creating new C++/CLI project

 

You can use the following project and solution names if you want else change accordingly.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: creating new C++/CLI console application in Visual Studio

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: the standard C++/Cli console application project tempalte

 

Adding the ASP .NET/C# Web Service Reference

 

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.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: adding the web service reference created using ASP .NET/C# code to the existing C++/CLI project

 

In the Add Web Reference page, select the second link: Web services on the local machine.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: finding and selecting the web services hosted on local web server

 

Next, select the Service1 link.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: selecting the desired web services

 

You can test the exposed methods at this stage. Leave the web reference name to 'localhost' and click Add Reference button.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: adding the web service reference to the current project

 

The added web service reference or normally called proxy class is visible in the Solution Explorer.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: the ASP .NET/C# web service reference seen in the Visual Studio 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;

}

 

Build, Run and Test the Whole Project

 

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.

 

Consuming the ASP .NET/C# web service application using C++/CLI program example: a sample output for the addition operation using web service and the C++/CLI console mode application

 

 

 


< ASP .NET & VB .NET Web Service Program Example | Main | C#/ASP .NET Asynchronous Web Service Program Example >