Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET
Article

Invoking a Web Service Without Web Reference

Rate me:
Please Sign up or sign in to vote.
4.53/5 (35 votes)
27 Jun 20068 min read 299.1K   3.7K   81   41
How to support loosely coupled integration by invoking Web Services dynamically, without setting a Web Reference.

Introduction

This article describes how to call a .NET Web Service without having to use the Visual Studio .NET 2005 IDE to set a Web Reference to the Web service.

Background

This is a fair enough requirement that you might come across in multiple scenarios. Here are some situations, that come to my mind, where this approach might come handy:

  • Loosely coupled integration in Application Service Provider Model - Application Service Provider Model is a model where a single web application can support users from multiple clients. In this scenario, there is often a need to allow the application to call different web services depending on the client, and it may not always be a good idea to do a web reference to each and every one of those web services, specially when they have exactly the same specification.
  • Dynamically invoking different Web Services during runtime - There are two or more completely different web services with the same WSDL specification but different implementation inside functions and business logic. If the application needs to decide which web service to invoke depending on the flow / business logic, during runtime, then it may be a good idea to take up this approach.

Basically, I wrote this article because I found that some of the methods that I found in CodeProject and the Web were too simple and not accurately correct, while others were a little more complicated and elegant, which were highly useful for more complex scenarios, and more often than not they were either of the two scenarios I describe in the bulleted list above or variations of them thereof that might motivate developers to call a web service dynamically without manually setting a web reference to it.

The Example

For the sake of simplicity, let's keep this example very simple. We will avoid all database interaction code and try to focus on solving our problem here - which is to call a web service without setting a web reference in the Visual Studio .NET 2005 IDE. So, let's assume that a hypothetical application called "WelcomeUser" wants to follow the Application Service Provider Model, and wants to allow users of two different companies, "Foo" and "XFoo", to sign on to the application.

However, the real complication here is that the WelcomeUser web application wants to let the companies Foo and XFoo decide the welcome message they want to display to their users, by invoking a web service which both Foo and XFoo host and maintain independently in their infrastructure environment. In other words, the development team of the WelcomeUser Web application does not want to be responsible for the maintenance, deployment, and implementation of web services for each client, and yet wants to support loosely coupled integration with these web services.

Even though the whole idea of both these companies writing web services just to display a welcome message sounds overly simplistic and stupid, let us just assume that the welcome message is driven by complex logic which is based on different databases which are hosted in the environments of Foo and XFoo.

Design the WSDL, Distribute to Clients, They Implement Their Own Web Services

Since the WelcomeUser application needs to call completely different web services depending on which company the current user belongs to, the WelcomeUser application development team needs to decide the WSDL specifications that both Foo and XFoo need to follow when they are writing their web services. Even though this analogy is not fully correct, for now, it would be safe to say that this WSDL that the development team of the WelcomeUser application decides will act as a contract for this loosely coupled integration, something like an Interface in the object oriented world, if you will.

In order to create this WSDL, contact the programmers of the WelcomeUser application, decide to write a Web Service which they will call "WelcomeMessageStub", which will have a welcome method called "GetWelcomeMessage()". The following code illustrates this:

C#
[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";
    }
}

Once this is done, we use the WSDL specifications of this web service by visiting "[URL Of the Web Service]/WelcomeMessageStub.asmx?wsdl", and publish the WSDL specifications to all clients including Foo and xFoo.

I should point out here that there are multiple tools that allow you to generate WSDL directly without having to code a stub Web service. So, coding the stub Web Service to get a WSDL is just a quick and dirty way of doing this. I do this here because I want to keep the focus on the primary problem which is to invoke multiple web services without setting the web reference to these web services.

Now, let us assume that developers at Foo write the following implementation of our WSDL specification and write a web service WelcomeMessageFoo:

C#
[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, however, takes the WSDL and writes a completely different implementation of the Web Service:

C#
[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!";
    }
}

Assuming that both the different web services are hosted at completely different URLs, the WelcomeUser Web application which follows the Application Service Provider Model needs to be able to invoke different web services from different URLs depending on the user who is signing in. In other words, if the user signing in belongs to company Foo, the application needs to reach out to the URL of the WelcomeMessageFoo service and call its GetWelcomeMessage, where else if the user belongs to company XFoo, the applications needs to reach out to the WelcomeMessageXFoo Web service and call its GetWelcomeMessage method.

Even though this example sounds really simplistic, this approach becomes important because there may be multiple clients in the future and it may not be always possible to set a web reference to thousands of clients separately. In fact, setting the Web Reference for hundreds of different clients separately may not be such a good idea. The sections that follow in the article explain.

Writing the Application Service Provider Application, WelcomeUser

Before we get started, we need to understand some basics of how web services work and the basics of what happens when you set a web reference using the Visual Studio .NET 2005 IDE. When you set a web reference to a Web Service using the Visual Studio .NET 2005 IDE, the IDE actually creates proxy classes for you, based on the WSDL specifications, which represent the Web Service classes on the remote server.

If you've ever wondered how you get Intellisense with Web services, and why the Intellisense is not accurate till you update your web reference each time you change your web service, it is because these proxy classes are being used by Visual Studio to offer you Intellisense. In fact, when you invoke a Web method through a client, all you are doing is invoking a method in one of these proxy classes which are just local to your project. These classes, in turn, are capable of invoking the remote methods on the Web Service URL.

So, every time the web service is updated, these proxy classes need to be regenerated by clicking Update Web Reference. However, the proxy classes do not always have to be generated from the IDE, .NET also gives us the WSDL.exe utility to manually generate these proxy classes and include them in your project.

So after this long story of what Visual Studio .NET does and the fundamentals of how Web Services work, let's get back to what we were doing, which is to start writing our WelcomeUser Web application. The first thing that we will need to do is to create a proxy class for our WelcomeMessageStub web service. We will then use this proxy class to invoke web services that Foo and xFoo have written independently at completely different URLs.

To create the proxy class for the WelcomeMessageStub Web Service, let's use the WSDL.exe utility and fire the following command at the command prompt:

Image 1

Notice that WSDL.exe creates a proxy class, WelcomeMessageStub.cs, for me based on the WSDL specifications. With this done, lets quickly create a new website for the WelcomeUser Web application and place the code in the App_Code folder of this site.

Now that this is done, it might be a good idea to dissect the generated proxy class code. To keep this article focused on the problem, we start off from the first place, and let's just look at the relevant parts of the generated proxy class. Illustrated below is the code generated for the default constructor of the generated proxy class:

C#
public WelcomeMessageStub<CODE>()</CODE> {
    this.Url = "http://localhost/WelcomeMessageStub/WelcomeMessageStub.asmx";
}

It's worth noting here, that the default constructor of this class contains the URL of the asmx file where the web service is hosted. However, the URL property of this class is both public and read/write, which means that it can be modified during runtime.

The code snippet below illustrates how the web services of the clients Foo and xFoo can be invoked dynamically during runtime, using the proxy class:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // We Can use Database or other Mechanisms 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 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 could Hit xFoo's Web Service
        // URL to Invoke the right Web Method on the fly.
        // The URLs can be COMPLETELY different as
        // long as they are reachable.
        welcomeMessageStub.Url = 
          "http://localhost/WelcomeMessageStub/WelcomeMessagexFoo.asmx";
        Response.Write(welcomeMessageStub.GetWelcomeMessage());
    }
}

Next Steps

This article describes how multiple web services with the same WSDL specifications can be invoked dynamically without requiring to set a web reference to each and every web service individually. In this article, we used WSDL as a contract between the Web Service provider and the consuming application. In the next article, we will discuss how we can use XSD to have better contracts between the Web Service provider and the consumer.

History

  • Saturday, June 24, 2006 - Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
Rajiv has been developing on Microsoft Platforms for over 6 years and is a principal officer, technology @ eFORCE Inc. where he has worked for around 5 years and enjoys doing some really interesting stuff like writing a software adaptor for MFD scanners and other hardware using Microsoft Technologies, Writing a DAL that hooks up VC++ layers to .NET, writing a .NET Remoting Bridge that securely connects world-wide oil rigs of a drilling company and many other interesting endeavours.

More information about Rajiv can be found @ www.rajivpopat.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Devesh Somvanshi16-Nov-13 3:24
Devesh Somvanshi16-Nov-13 3:24 
QuestionProblem with REST Pin
LeonardoMiller13-Sep-12 13:04
LeonardoMiller13-Sep-12 13:04 
GeneralMy vote of 4 Pin
wwuwwei1-Oct-11 0:24
wwuwwei1-Oct-11 0:24 
GeneralInvoking a Web Service Without Web Reference Pin
blazeraja17-Jun-11 0:44
blazeraja17-Jun-11 0:44 
GeneralMy vote of 5 Pin
blazeraja17-Jun-11 0:39
blazeraja17-Jun-11 0:39 
Generalvote of 5 [modified] & Pseudo-Class Example Pin
MacSpudster26-Jan-11 6:56
professionalMacSpudster26-Jan-11 6:56 
GeneralMy vote of 4 Pin
sriramcse3121-Oct-10 20:12
sriramcse3121-Oct-10 20:12 
Generalwsdl is not recognized... when creating stub class Pin
Member 312107416-Feb-10 18:19
Member 312107416-Feb-10 18:19 
GeneralAnother scenario where adding a Web Reference is NOT appropriate Pin
cathal_mchale12-Nov-08 0:14
cathal_mchale12-Nov-08 0:14 
GeneralFor Web Service terms and Terminology in .net Pin
DotNetGuts10-Sep-08 12:45
DotNetGuts10-Sep-08 12:45 
GeneralGot "Object reference not set to an instance of an object" Error after deplayment Pin
yano300028-Feb-08 3:46
yano300028-Feb-08 3:46 
GeneralMultiple asmx service to a single service in IIS Pin
Member 336106020-Jan-08 20:48
Member 336106020-Jan-08 20:48 
GeneralProject Deployment Pin
mev2912-Aug-07 20:49
mev2912-Aug-07 20:49 
GeneralAdding a single Web Reference works if writing a desktop application Pin
picazo12-Jul-07 12:16
picazo12-Jul-07 12:16 
GeneralHelp me ! Pin
hoang2th3-Apr-07 5:33
hoang2th3-Apr-07 5:33 
GeneralRe: Help me ! Pin
rajivpopat9-Apr-07 2:31
rajivpopat9-Apr-07 2:31 
GeneralExeception while accessing Web service Pin
nikhil123415-Feb-07 18:28
nikhil123415-Feb-07 18:28 
GeneralRe: Exeception while accessing Web service Pin
rajivpopat15-Feb-07 20:27
rajivpopat15-Feb-07 20:27 
GeneralRe: Exeception while accessing Web service Pin
nikhil123415-Feb-07 20:54
nikhil123415-Feb-07 20:54 
GeneralRe: Exeception while accessing Web service Pin
rajivpopat22-Feb-07 22:03
rajivpopat22-Feb-07 22:03 
Questioncreate an override for the constructor? Pin
steve_dee12-Jan-07 11:28
steve_dee12-Jan-07 11:28 
AnswerRe: create an override for the constructor? Pin
rajivpopat20-Jan-07 7:31
rajivpopat20-Jan-07 7:31 
GeneralNice Article (But one problem if same method definition) Pin
ims62328-Dec-06 15:51
ims62328-Dec-06 15:51 
GeneralRe: Nice Article (But one problem if same method definition) Pin
rajivpopat20-Jan-07 7:38
rajivpopat20-Jan-07 7:38 
Questioncan I make a WSDL object and use at run time Pin
chino11213-Sep-06 2:22
chino11213-Sep-06 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.