Invoking Web Method from one Web Service to Another

Description

Today, we have a lot of idea about a web service. Many of us are already using web services for various purposes. I am not going to discuss the usage of web services. In this article I will show you how to connect a web service from another. There are many occasions when a developer need to connect to one web service from another.

In my previous submission, I showed how to create and deploy a complete Visual Shop web service. In that application, a customer can buy books. In a customer has to pay for the books, I called another service Bank web service. So basically the Bank web service is different than the Virtual shop. The point here I am trying to make is how to we call methods of one service from another. That's what we are going to see in this article in a moment.

We can invoke webmethod  by using three method:

  1. using RSA Encryption as the security layer between the three different web 
  2. Access then as if its a separate web service (using proxy client) 
  3. Use  libraries (.dll) and let each web services use the service of the dll.

Here we already have an idea about proxy client. By using proxy client we can invoke webservice the way we want. I am interested in showing you today is  how we can build a dll for each webservice and use it in another webservice. By using VS.NET we can do  it very easily. 

Lets build two simple web services in visual Studio .NET 7.0. I think you already know how to make a web service in VS 7.0. Build a web service using VS.NET and name it  calculator, which have two webmethod ADD and Mul. Add these two methods. The methods looks like the following: 

[WebMethod]
public int ADD(int a,int b)
{
return a+b;
}
[WebMethod ]
public int Mul(int a,int b)
{
return a*b;
}

To test it, just press F5. If you are using windows XP then you dont have PWS, no VS by default made a copy in inetpub -> wwwroot directory  as websevice1.

Now lets make a second webservice named webservice 2 and add one webmethod there 

[WebMethod]
public int Test (int a,int b)
{
WebService1.Service1 testadd =
new WebService1.Service1();
return testadd.ADD(a,b);
}

oh may be you can understand whats going on. By default VS 7.0 always creates a dll for each webservice. If you check WebService1 folder in inetpub directory, in bin sub  directory you can find webservice1.dll file. We just add this file in webservice2. Pretty easy huh? Now find solution explorer from upper right hand side which looks like the following figure:

InvokingWebMethodsDSA1.jpg

Now right click on references and choose add references. Now appear another window looks like the following figure:

InvokingWebMethodsDSA2.jpg

You use the browse button and choose webservice1.dll file and you will see it in the list.

Now next step is to add reference to the service. We add namespace using the following code:

using webservice1;

I guess you can understand whats going on next. Now we create an object and call its methords to invoke webmethod from webservice1. Now test it .working absolutely.

That's it. Now we can invoke webmethod from another webservice. You can also use RSA for connecting web services, but I am waiting for Prasad who will discuss about these matter. He will be coming soon. I would also like to thank to Manish Mehta to help me on giving me an idea.

Up Next
    Ebook Download
    View all
    Learn
    View all