Create A Client Web Page For A Web Service

To consume a Web Service, you can create Web Service Clients. ASP.NET framework creates clients for you automatically if you access the Web Service file (*.asmx) directly. For your custom applications, you need to create your own client to call Web Services.

You need to walk through a couple of steps to enable a Client Web Page. The first is to create a service definition file. This can be created 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 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 compile the class, you can then import the namespace of the Web Service into the Web Page that will consume the Web Service. On the client Web page, you create an instance of the Web Service just like any other .NET object. After you create an instance, you can call methods from the Web Service.

CREATE A CLIENT WEB PAGE FOR A WEB SERVICE

HCAWINNTASystem32\cmd-ex.

Microsoft Windows 2DDD [Uei CC) Copyright 1985 2000 Mil

C:\>cd C:\InetPub\wwwroot n 5.00.2195] oft Corp.

C:\Inetpub\wwuroot>wsdl http://localhost/SiwpleWebSer»iice.asmx?MSDL Microsoft (KJ Web Services Description Language Utility [Microsoft .NET Framework Version 1.D.2615.1] My Computer ¡Copyright (C) Microsoft Corp 2000. All rights reserved.

C:\Inetpub\wwuroot\SimpleWebSeroiee.es Writing file 'C:\InetpubWwroot\SimpleWebService.cs'.

C:\InetDub\n»Ajroot>csc /out:bin\SimoleWebService.d11 /tilibraru /r: MyNetwork TISIML.d 11 /riSystem.Web.Seroices.dll SinpleUebService.es

Place» ¡Microsoft CR) Visual Ctt Compiler Version 7.00.91i»8 [CLR version v1.0.2615]

C:\Inetpub\wfcwroot>_

oft Corp 2000. All i

C:\Inetpub\wfcwroot>_

D Open the command prompt.

—0 Change directories to where the Web Service is located.

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

—Q Compile the proxy class to the /bin directory.

■ The proxy class is created and compiled.

0 Open

GenericTemplate.aspx from the Code Templates directory.

0 Add a form to the page.

^O Add a Label control to the form.

0 Open

GenericTemplate.aspx from the Code Templates directory.

0 Add a form to the page.

^O Add a Label control to the form.

te 1 It 1

You can also pass data to a Web Service from a control on a form on a Web page. To run this code, you need to run the ClientWebService_ai.bat that creates and compiles the proxy classes for this code.

TYPE THIS (IN THE SERVER SCRIPT BLOCK):

protected void Page_Load(Object Src, EventArgs E){ if (!Page.IsPostBack){

SimpleWebService simplewebserviceMessage = new SimpleWebService(); string stringMessage = simplewebserviceMessage.SayWelcomeStatement(); labelMessage.Text = stringMessage;} else{

ParameterWebService parameterwebserviceMessage = new ParameterWebService(); string stringMessage =

parameterwebserviceMessage.SayWelcomeStatement(textboxName.Text); labelMessage.Text = stringMessage; }}

RESULT:

1

A Web page displays the greeting. If you submit the page with your name, it will call a different Web Service that customizes greeting with your name in it.

I J

-Q Add the Page_Load function to the page.

± Save the file and request it from the Web server.

■ A welcome message from the Web Service appears.

-Q Add the Page_Load function to the page.

—0 Create a new instance of SimpleWebService.

I Set the result of calling the SayWelcomingMessage from the

SimpleWebService into a string variable.

Update the Label control with the result.

± Save the file and request it from the Web server.

■ A welcome message from the Web Service appears.

Was this article helpful?

0 0

Post a comment