Consuming web service in classic ASP application using .Net component

It’s fairly simple to consume web service in your classic ASP application using wrapper component developed in .net world.

So first create a wrapper component that will consume the web service.
Follow below given steps:

1. Open visual studio and create a Class Library project in C#.
2. Name the project WebServiceWrapper and the class as Wrapper.
3. Paste below code in the Wrapper.cs class window.

-----------------------------------------
using System;

namespace WebServiceWrapper
{

public class Wrapper
{

MyWebService.Service1 svr;

public Wrapper()
{
}

public void Initialize(String url)
{
svr=new WebServiceWrapper.MyWebService.Service1();
svr.Url=url;
}

public String URL
{
get{return svr.Url;}
set{svr.Url=value;}
}

public String HelloWorld(String str)
{
return svr.HelloWorld(str);
}

public String HelloWorld(String str, String url)
{
svr=new WebServiceWrapper.MyWebService.Service1();
svr.Url=url;
return svr.HelloWorld(str);
}

}
}
-----------------------------------------

4. Right click on the project and choose Add Web Reference option.
5. In the Add Web Reference window browse your web service and give the web reference name: MyWebService and click on Add Reference button.

Note:- In the above code web service in question(Service1) is hosted on local machine and has a method named HelloWorld that takes string param and return string value but you have to replace this code with your web service name and web method accordingly.

6. Build the solution that will create WebServiceWrapper.dll
7. Now you have to create type library of your .net component and register that to make it available in your VB COM component. For that use following command at VS .Net command prompt window.

regasm.exe < path of WebServiceWrapper.dll > /tlb /codebase

It will create a type library named WebServiceWrapper.tlb at the dll location and register the same as well. Ignore any warning message shown.

You are done with creating your .NET wrapper component. Now time to use it in your VB COM component.
Here I assume that you would have gone through my previous post "How to create ASP application using ASP Object Library" becasue here I will tell you what changes are needed to do in your ASPComp component that you would have created earlier.

So Open your ASPComp project and add following code in the clsASP class.
-----------------------------------------
Public Sub CallWrapper_HelloWorld()

Dim oclient
Dim strg As String
On Error GoTo ErrorHandler
Set oclient = CreateObject("WebServiceWrapper.Wrapper")
oclient.Initialize "http://localhost/WebService1/Service1.asmx?wsdl"

strg = oclient.HelloWorld("Sanjay")
oResponse.Write strg & vbCrLf

Exit Sub

ErrorHandler:

oResponse.Write "Invalid Web Service Request. Please check the Web Service URL: " & Err.Description

End Sub
----------------------------------------
Note:- Replace the web service url with your web servce url.

Now go to Project-> reference menu and add reference of WebServiceWrapper Type Library that you had created earlier by browsing to its location.

Now re-make ASPComp.dll to incorporate new changes in the COM component.

Note:- You will have to re-register the ASPComp.dll

After completing the COM component changes, open your asp page in the notepad and replace the method call with the newly created method.

So your MyASP.asp page will look like:

< %
Set oExample = Server.CreateObject("ASPComp.clsASP")
oExample.CallWrapper_HelloWorld
% >

No time to test user efforts. So browse http://localhost/ASPTest/MyASP.asp in the IE window.

No comments: