< Web Service 1 | Index 1 | Index 2 >


 

 

Building a Web Service 2

 

 

What we have in this page?

  1. Using the Web Service from a Browser

  2. Using the Web Service from Code

  3. If You’re Not Using Visual Studio .NET

  4. A Very Quick Reference

 

 

Using the Web Service from a Browser

 

You can use a Web browser to invoke the Web service methods, and the following short exercise will show you how.

 

7.      Start Microsoft Internet Explorer or other browsers.

8.      Type the URL for the Web service’s .asmx file into the Address bar. If you’ve named the Web service Converter and installed it on IIS on your local computer in the default virtual directory, the address will be http://localhost/Converter/Converter.asmx. If you’ve installed the service on a remote computer or used another virtual directory, you’ll have to amend the address accordingly. However you may encounter the following errors.

 

A name was started with an invalid character ... error seen in Internet Explorer

 

 

Server Application error seen in Internet Explorer

 

Well, there are many reasons for these errors. We are not sure about it because there are different OS, ASP.NET and .NET framework versions used. You can surf the Internet for these reasons. In our case we do the following:

 

a.       Open a command prompt and navigate to this directory (this is .NET version 2.0.x yours may be different).

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

b.      Run these commands in this order from command prompt:

1.      aspnet_regiis.exe –u  (uninstall)

 

2.      aspnet_regiis.exe –i  (re-install)

 

3.      aspnet_regiis.exe –e (enable)

c.       Then, restart IIS

 

9.      When you press ENTER for the http://localhost/Converter/Converter.asmx address, the Web server will return you details of the Converter service and the methods it supports.

The details of the Converter service and the methods it supports seen through Internet Explorer Web browser

 

10.  You can see how metadata has been extracted from the class and used to provide a description of the service.

11.  Click one of the two methods. An HTTP GET command is sent to the server, which results in the server auto generating a Web service consumer and sending it back to the browser. The consumer takes the form of an HTML page that contains a description of the service, a way of testing the method, and descriptions of how to use the service using HTTP GET, HTTP POST, and SOAP.

 

ConvertC2F method seen in IE browser

 

ConvertF2C method seen in IE browser

 

12.  Test the method by entering a suitable value into the text box and clicking Invoke. The Web service will invoke the method and return the answer as XML.

<?xml version="1.0" encoding="utf-8" ?>

  <double xmlns="http://VCSBS/WebServices/">21.111111111111111</double>

 

Testing the Celsius degree to Fahrenheit degree and vice versa through web service application

 

The double element identifies the value as a double and the namespace is used to identify the Web service that is returning the result.

 

 

 

 

Using the Web Service from Code

 

Before you can use a Web service, you obviously need to locate it and find out how you need to use it. Visual Studio .NET uses Web references to represent Web services wherever they are located. It also has tools to locate services, and you can browse your local computer; a Universal Description, Discovery and Integration (UDDI) server; or the Internet in general. UDDI is a mechanism for Web service providers to advertise the existence of their services and for clients to locate Web services of interest. When you find a service that you want to use, Visual Studio .NET will use the DISCO discovery service to retrieve the WSDL file that describes the service. Client code needs to be able to find the Web service at run time and to communicate with it. Adding a Web reference to your project uses the WSDL information to create a proxy class that takes care of finding and communicating with the service, so all you have to do is work with a local object that exposes the same methods as the remote service. If you’ve ever used remote COM objects from Microsoft Visual C++ code, you’ll see similarities in the way you use Web services.

The following exercise shows you how to use the Web service that you created earlier in the module. This exercise assumes that you have built and installed the service and that it’s running on a local Web server.

 

1.      Create a new Visual C++ CLR Console Application project named TempClient. Leave the definition of the main() function as it is because you won’t require any command-line arguments. Just delete the given Hello World code.

 

Creating a new Visual C++ CLR Console Application project named TempClient

 

2.      Add a Web reference to the project, either by choosing Add Web Reference on the Project menu or by right-clicking the project in Solution Explorer and selecting Add Web Reference from the shortcut menu. This will bring up the Add Web Reference dialog box.

 

Adding a Web reference to the project, either by choosing Add Web Reference from Project menu or Solution Explorer short-cut

 

Add Web Reference form

 

3.      You have three ways to locate the Web service. First, if it’s registered with the Microsoft UDDI service, you can click the UDDI links. Second, if you know the location of the service, you can enter a URL in the Address bar. If you installed the Converter service on a remote machine, enter the URL. Otherwise, the service lives on the local server, so you can use the third option and click the linked phrase Web services on the local machine to display locally registered services.

It might take several seconds to activate this link while the server uses dynamic discovery to build its list of services.

Details of the dynamic DISCO file for the local server, which links through to the local Web services will display. As you’ll see from the following figure, we only have the previous web service installed.

 

Finding Web Services using Web Reference form of the Visual Studio 2005

 

4.      Find the Converter service in the list if there are many web services hosted on your machine, and click it. The same test page that you’ve already seen will display.

 

Finding the Converter service using Web Reference form of the Visual Studio 2005

 

The Converter web service seen in the Web Reference form

 

You can use the test page to retrieve more information about the service and see how the methods work.

 

5.      Because we know that the service works and what it does, click the Add Reference button to create the reference proxy.

When the process has finished, you’ll find that a new file named WebService.h has been added to the project, which simply includes a file named localhost.h. You’ll find that you get a header file generated for each Web service host. Because this example uses a service from the local machine, code is placed in a file named localhost.h. This header file defines #using directives and the definition for the proxy class. You’ll also see that a namespace localhost is used to contain the ConverterClass class. The namespace is named after the Web service host and is included so that you won’t get name clashes if you reference Web services with the same name on different hosts.

 

The added files of the Web service seen from the Solution Explorer window of the Visual Studio 2005

 

The proxy code has been generated in C++, and if you want, you can browse the code in localhost.h to see how the proxy has been implemented.

In Visual Studio .NET 7, the proxy class was created in Microsoft Visual C# and compiled into a separate DLL. Version 7.1 creates the proxy in C++ and compiles it into the project, so there is no separate proxy DLL. We are using .NET 8.0.

 

6.      Add an #include to include WebService.h in the TempClient.cpp file, and also add a using directive for the localhost namespace so that you won’t have to use fully qualified names.

 

Adding Webservice.h header file to the main project file

 

 

Adding the localhost namespace to the main project file

 

7.      Add code to the main() function to create and access the Web service.

int main(array<System::String ^> ^args)

{

      Console::WriteLine(L"Web Service Test");

    try

    {

        // Create a proxy object

        ConverterClass^ conv = gcnew ConverterClass();

        // Call a conversion method...

        double fahrVal = conv->ConvertC2F(10.0);

        Console::WriteLine(L"10C is {0}F", (Object^)fahrVal);

    }

    catch(Exception^ pe)

    {

        Console::WriteLine(pe->Message);

    }

    return 0;

}

The proxy class that has been provided by you has the same name as the deployment project, so the class name here is ConverterClass.

You can create a new ConverterClass object just like any other .NET object and call its ConvertC2F method to convert a value. This might not seem like a particularly impressive demonstration, but remember that the Web service could be installed anywhere on the Internet and could be written in any language. In fact, it’s not even necessary for the service to be written using the .NET Framework at all.

 

8.      Build and run your program. The following output should be expected. 10 °C has been converted to the 50 °F.

 

Using web service through the C++ .NET code program output sample

 

 

 

 

If You’re Not Using Visual Studio .NET

 

You can still use Web services even if you’re not using Visual Studio .NET. The wsdl.exe command-line tool supplied as part of the .NET Framework SDK will generate a proxy for you in exactly the same way as the wizard does in Visual Studio .NET. The following command line shows how you could create a proxy by hand for talking to the Converter service:

wsdl /l:cs /protocol:SOAP http://localhost/Converter/Converter.asmx?WSDL

running the WSDL tool from the command prompt

 

The /l (or /language) argument determines the language the proxy will be written in. You’ve got a choice of Microsoft Visual Basic (VB), Microsoft JScript (JS), or Visual C# (CS), but not C++. The default is CS if you don’t specify a language. The /protocol argument defines the protocol that’s going to be used to talk to the service. We’ve chosen SOAP, but you can also specify HttpPost or HttpGet. The final argument is the URL of the Web service, and you can also specify a WSDL file if you have one available. You can build the proxy code into a DLL using the following command line (the csc.exe is in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder for our Windows XP Pro SP2):

csc /t:library /r:system.web.services.dll /r:system.xml.dll /r:system.data.dll Converter.cs

The command line tells the C# compiler to compile Converter.cs into a DLL and gives the references to the DLLs containing the .NET classes that will be used. Once you’ve built this DLL, you can add #using to any project to use the Web service.

 

A Very Quick Reference

 

To

Do this

Create a class that acts as a Web service.

Create an ASP.NET Web service project. You can also add the WebServiceAttribute to the class definition to specify a namespace and description for the service. For example:

 

[WebServiceAttribute]

class MyClass {...};

 

Add the WebMethod attribute to the member functions that you want your Web service to expose. For example:

 

[WebMethod]

String^ GetMyName() {...}

 

Create a class that acts as a Web service and gives access to the standard ASP.NET objects.

Create a managed C++ class that inherits from System::Web::Services::WebService.

Add a description to a Web service class or a Web method.

Add a Description attribute to the WebServiceAttribute or WebMethod attribute. For example:

 

[WebServiceAttribute (Description= "My Service")]

class MyClass {...};

 

Provide a unique identifier for your Web service.

Use the Namespace attribute on the WebServiceAttribute for the Web service class to define a unique namespace URL. For example:

 

[WebServiceAttribute (Namespace="http://myServiceUrl")]

class MyClass {...};

 

Use a Web service from managed C++ code.

Add a Web reference to the project by choosing Add Web Reference on the Project menu or by right-clicking the project name in Solution Explorer and selecting Add Web Reference.

Create a proxy to a Web service when you’re not using Visual Studio .NET.

Use the wsdl.exe tool from the .NET Framework SDK to create a proxy from the WSDL description of the service, and then compile the code into a proxy DLL.

 

Table 9

 

 

 

Part 1 | Part 2

 

 


< Web Service 1 | Index 1 | Index 2 >