Changing Target Web Service At Runtime

zhaozj2021-02-16  60

Introduction

With the continuous development of the web service, its client references are also constantly developing, timely, dynamic basis. The URL of the .asmx file is trend to add web references.

When we load a web service, this web service may replace the URL address. This will make us quote the service invalid, this is what we don't want to see. So how can web applications continue to take effect after changing the URL address of Web Services?

The following list will tell you.

First we create a web service with only a Mothod.

Create a new C # web service project in VS.NET Open the default .asmx file and add following code to it using System; using System.Web.Services; namespace HelloWorldWS {public class CHelloWorld:.. System.Web.Services.WebService { [WebMethod] public string getHelloWorld () {return world from chelloworld ";}}}

. As shown above this web service class (CHelloWorld) contains a single method called GetHelloWorld () that returns a string Add another .asmx file to the project Open the file and modify it as shown below using System..;

Using system.Web.services;

Namespace HelloWorldws

{

Public Class ChelloWorldBackup:

System.Web.Services.WebService

{

[WebMethod]

Public string getHelloWorld ()

{

Return "Hello World from ChelloWorldBackup";

}

}

}

This class is similar to previous one but its name is CHelloWorldBackup. Also, it returns different string from GetHelloWorld () method so that you can identify the method call Now, that we have both the web services ready compile the project. Next, we create a client Diffuse program to load this web service

Create a new ASP.NET web application in VS.NET. The application will have a default web form. Before writing any code we need to add a web reference to our web service. Right click on the references node and select Add web reference. Follow the same procedure as you would have while developing normal web services Adding a web reference will generate code for proxy web service object Place a button on the web form and add following code in the Click event of the button:.. private void Button1_Click

(Object Sender, System.EventArgs E)

{

Localhost.chelloworld proxy = new localhost.chelloworld;

Response.write (Proxy.getHelloWorld ());

}

Above code shows how you will normally call a web service. The web reference contains information about the location of the web service. If you move the .asmx file after you depoly this client, it is bound to get an error. To avoid such situation Modify Above Code As Shown Below:

Private void button1_click

(Object Sender, System.EventArgs E)

{

Localhost.chelloworld proxy = new localhost.chelloworld;

Proxy.url = "http://localhost/webserviceurlandtimeout/helloworld.asmx";

Response.write (Proxy.getHelloWorld ());

}

In above code we have explicitly set Url property of the proxy class to the required .asmx file. You can easily store this URL in section of web.config file and retrieve it at run time. Now, even if you move your Web service, all you need to do is change it web. FOLLOWING CODE SHOWS THIS:

Private void Button1_Click (Object Sender, System.Eventargs E)

{

Localhost.chelloworld proxy = new localhost.chelloworld;

Proxy.url = getURL ();

Response.write (Proxy.getHelloWorld ());

}

Public String getURL ()

{

Return ConfigurationSettings.AppSettings ["WebServiceURL"];

The Web.config Looks Like this:

Key = "WebServiceURL"

Value = "http: // localhost / WebserviceurlandTimeout

/HelloworldBackup.asmx "/>

转载请注明原文地址:https://www.9cbs.com/read-24231.html

New Post(0)