Application For A Web Service

You can use a Console Application as a test harness for a Web Service. ASP.NET Web Services can be easily consumed by WinForm Applications or Console Applications.

Accessing a Web service from a console application requires many of the same steps as accessing it from a Web page. The first step is to create a service definition file by using the disco command and passing the URL to the Web Service. This will create a service definition file. You then need to use this service definition file to create a proxy class for the Web Class. You can use the wsdl command to pass the service definition file name to it. The result of running this command is a proxy class that you now need to compile to the bin directory as a library. You can use the csc command to compile the class.

After you have compiled the class, you can then import the namespace of the Web Service into the Console Application that will consume the Web Service. Within the Console Application, you can create an instance of the Web Service. After you have created an instance, you can call methods from the Web Service and display results to the console.

CREATE A CLIENT CONSOLE APPLICATION FOR A WEB SERVICE

CREATE A CLIENT CONSOLE APPLICATION FOR A WEB SERVICE

D Open a new document in your text editor.

< Create a new class. —Q Create the Main function.

—0 Create an integer variable in the Main function.

Q Set the result of calling the UpdateApplication Counter from the ApplicationWebService into an integer variable.

D Open a new document in your text editor.

^0 Import the System namespace.

< Create a new class. —Q Create the Main function.

—0 Create an integer variable in the Main function.

—0 Create a new instance of ApplicationWebService.

Q Set the result of calling the UpdateApplication Counter from the ApplicationWebService into an integer variable.

0 Format and write the result to the command line.

You can also pass data to the Web Service from the Console Application via a command line parameter. To run this code, you need to run the ConsoleWebService_ai.bat that creates and compiles a proxy class and Web Service Client for this code.

TYPE THIS:

using System;

class ConsoleWebService{

public static void Main(string[] args) {

int intlnitialize = Convert.ToInt32(args[0]); if (intlnitialize >= 0) { ApplicationWebService_ai applicationwebserviceCounter = new

ApplicationWebService_ai();

string strHitCountMessage =

applicationwebserviceCounter. UpdateApplicationCounter(intlnitialize);

Console.WriteLine(strHitCountMessage);

RESULT:

C:\>ConsoleWebService_ai.bat C:\ >ApplicationWebService_ai.exe 10 This service has been accessed 10 time(s).

Copyright CO Mit

ED C:\WINHT VSystem32\cmd.exe_[

Microsoft Windows 2DD0 [Version 5.DD.2195] (C) Copyright 1985-2000 Microsoft Corp.

C:V>cd C:\InetPiibWwroiit]

C:\Inetpub\wwwroot>wsdl http://localhost/ApplicationWebServiee.asmxPWSDL Microsott CK) Web Serwices Description Language Utility [Microsoft .NET Framework Version 1.D.2615.1] Copyright CO Microsoft Corp 2DDD. DU rights reserved.

C:\Inetpub\wuurootVflpplicationWebService.cs

Writing file 'C:\InetpubWwroot\fipplicationWebService.cs'.

C:\Inetpub\wwwroot>csc /tlexe /r:System.Web.dll,System.XML.dll,System.Web.

Services.dll ApplioationWebSeryice.es ConsoleWebSeryiee.es

Microsoft CR) Visual Ctt Compiler Version /.DD.91<i8 [CLR version vl.D.2615]

Copyright CO Mit oft Corp 2DDD. All i

Open the command prompt.

-E Change d rectories to where the Web Service is located.

—— Run the wsdl command to create a proxy class for the Web Service.

Microsoft Windows 2000 [Version 5.00.2195] CO Copyright 1985-2000 Microsoft Corp.

C:V>cd C :\InetPubViwnuroot

C:\Inetpub\v^Mroot>wsdl bttp://localhost/ApplicationWebService.asmx?WSDL Microsoft (R) Web Services Description Language Utility [Microsoft .MET Framework Version 1.0.2615.1] Copyright (C) Microsoft Corp 2000. All rights reserved.

C:\Inetpub\wwurootVApplicationWebService.cs

Writing file ''C:\IiietpulAiwniiroot\ApplicationWebSeriiice.cs''.

C:VInetpub\wwwroot>csc /tlexe /r:Systeri.Web.dll,System.XML.dll,System.Web.

Services.dll ApplicationWebService.es ConsoleWebService.es

Microsoft <R) Visual C# Compiler Version 7.00.9148 [CLR version w1.0.2615]

soft Corp 2000. All rights

R • Wnpt pnh\ii«i«iii*nnt >Hnncn 1 eWphSpi-u i rp

This service has been accessed 2 times.I-

C:\InetpubVwwiiiroot>

C:\Program Files\Uisual ASP.Net\Chapter07\Code>issue with proxy class...

± Compile the console class and the Web Service.

■ The proxy class is created and compiled and the console application is compiled.

'—E Run the created executable a couple of times.

■ A message from the Web Service appears about the number of times the Web Service has been accessed.

Was this article helpful?

0 0

Post a comment