How to dynamically call Asp.net WebService

Source: Internet
Author: User

Original English article address: http://www.codeproject.com/kb/cpp/callwebservicesdynamic.aspx. (Some of them are not translated here if they do not affect understanding)

Abstract:

This article describes how to dynamically call WebService without adding a web application in vs2005 IDE.

Background:

You may encounter this kind of requirement in a variety of occasions. I can think of the following two methods that can be easily applied.

  • Loosely Coupled integration in Application Service Provider Model. Application Service Provider model indicates that a single web application can support users from different clients.

In this case, the application needs to call different independent WebServices according to different customers. If you want to add references to each WebService, this is not a good note, especially when these services have the same specifications.

  • Dynamic call of different WebServices during runtime. Assume that two or more WebServices have the same WSDL usage but different functions and internal implementations, this method is effective when the application needs to call the corresponding service according to the actual needs during runtime.

Example

For the sake of simplicity, we try to delete complicated logic parts. We have removed the code for interaction with the remainder database, focusing on the topic of this article: do not add web references and call WebService. Suppose there is a program called welcomeuser, which complies with the model standard in application service provider. This program requires users from different companies such as "foo" and "xfoo" to log in.

However, the actual application is a web program. It calls services deployed in Foo and xfoo and maintained independently to provide different welcome messages to users in different companies. Developers of this program do not want to deploy and maintain the services of different clients. They want to achieve loose coupling as much as possible.

Although it sounds a little difficult to let every company write their own idea of welcoming messages, we assume that this kind of welcome information is based on the complicated logic processing on the different database environments of the two companies.

Design WSDL, distributed clients, and their own implementations

Because the welcomeuser application needs to call different WebServices according to users of different companies, developers should determine the WSDL specifications that foo "and" xfoo should comply with before writing WebServices.

The WSDL low-coupling contract here is similar to the interface in OOP.

The developer immediately wrote a WebService named welcomemessagestub's inclusion method"GetWelcomeMessage()". The sample code is as follows:

[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WelcomeMessageStub : System.Web.Services.WebService{    public WelcomeMessageStub () { }    [WebMethod]    public string GetWelcomeMessage() {        return "The Welcome Message That You Need To Return";    }}

Next, we access"[URL of the Web Service]/welcomemessagestub. asmx? WSDL", To use and release the WSDL specification foo" and "xfoo clients of two companies. It should be noted that

Multiple tools can generate the WSDL specification without coding. The reason for this is to focus on solving the problems raised at the beginning of this article.

Let's assume that the developer of Foo implements the WSDL specification as follows:

 

[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WelcomeMessageFoo : System.Web.Services.WebService{    public WelcomeMessageFoo () { }    [WebMethod]    public string GetWelcomeMessage() {        return "Welcome to Foo!";    }}

Xfoo developers have different implementations as follows:

[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class WelcomeMessageXFoo : System.Web.Services.WebService{    public WelcomeMessageXFoo () { }    [WebMethod]    public string GetWelcomeMessage() {        return "Welcome to XFoo User!";    }}

Assume that the two different WebService hosts are in different URLs and all of them comply with the Application Service Provider Model. Specifically, a user from Foo needs the URL implemented by Foo

Users of xfoo also need to call their own URL implementation. Although this example is very simple, it is a common solution and suitable for complex situations.

Complete the welcomeuser Application

Before starting, let's take a look at the basic principles of WebService and the behind-the-scenes work that IDE does when you add WebService references in Visual Studio. NET 2005 IDE. When you set a WebService reference in IDE, IDE will provide you with a proxy class, which is based on the WSDL specification and represents the WebService class on the remote server. If you used to update a WebService reference each time, SMART awareness becomes less accurate

This is because the SMART awareness of WebService is a proxy provider generated by isual studio. When you call WebService on the client side, all you do is

The method of the proxy class is called locally. The proxy class actually calls the method of the remote Web Service URL. Therefore, the proxy class needs to be regenerated every time the WebService is updated. However, generating proxy classes using IDE is not the only one

Finally, netgave us the wsdl.exe tool to manually generate the proxy class.

Now that we have understood the principles of WebService, the processing done by IDE will start to write our welcomeuser application. First, we create the welcomemessagestub proxy class. Then use the proxy class

Call the respective URLs of Foo and xfoo.

Use wsdl.exe to perform the following operations on the command line:

 

Description Example: wsdl.exe is a welcomemessagestub. CS generated based on the WSDL specification. After that, we quickly create a website and put the code under the app_code directory.

At this point, we may need to analyze the generated proxy code. To focus on solving the problem, let's just take a look at the following code.

Public welcomemessagestub <code> () </code> {
This. url = "http: // localhost/welcomemessagestub. asmx ";
}
It is worth mentioning that this default constructor contains the URL of the WebService host. However, this URL can be dynamically changed at runtime. The following code snippet shows how the Foo and xfoo Client

Dynamic call proxy class,
Protected void page_load (Object sender, eventargs E)
{
If (! Page. ispostback)
{
// We can use database or other mechanic ms to select which company
// The user belongs to. Once this
// Is done we can have a small if construct
// To invoke the right Web Service from the right URL.
// Without going into those complications the below code displays how we
// Can call two different web services using the same proxy class.
Welcomemessagestub = new welcomemessagestub ();

// If the user belongs to foo-lets hit Foo's Web Service URL to invoke
// The right web method on the fly.
Welcomemessagestub. url =
"Http: // localhost/welcomemessagestub/welcomemessagefoo. asmx ";
Response. Write (welcomemessagestub. getwelcomemessage ());

Response. Write ("<br/> ");

// However if we found out that the user belongs
// Too xfoo we coshould hit xfoo's Web Service
// URL to invoke the right web method on the fly.
// The URLs can be completely different
// Long as they are reachable.
Welcomemessagestub. url =
"Http: // localhost/welcomemessagestub/welcomemessagexfoo. asmx ";
Response. Write (welcomemessagestub. getwelcomemessage ());
}
}
End

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.