HTML clipboard
I have come across the situation where I have the Web Service wrote during the
VB 6.0 using the Soap Toolkit from the COM component, I want to consume the Web
Service from the .Net Application (C# or ASP.net). There are few ways to consume
the Web Service either wrote from .Net version or any other application.
Here I would like to share how to consume the Web Service in .Net application.
As most of us know what is Web Service so I will not go deep into that.
I have generated the WSDL (Web Service Descriptor Language) from the COM (+)
component using the SOAP Toolkit. SOAP will generate the .WSDL, .WSMX and .asp
file which we can use to access the Web Service.
Step 1:
- Create the C# application (windows)
- Add the Web Reference and Include the WSDL file under the Web Reference
folder.
- Give the name to that Web Reference i.e. CallWS
- Set the Web Reference URL to something like
http://servername/virtualdirectory/services/APIEnterprise.WSDL
Now you have the Web reference added to your application. You can now access the
Web Service and exposed API's of that Web Service.
Open the form and add the button. On Button click event will access the Web
Service and Its Function.
To access the Web Service we have to create the Instance from the Web Reference
name.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace ConsumeWebService
{
public partial
class Form1 :
Form
{
public Form1()
{
InitializeComponent();
}
private void
btnShow_Click(object sender,
EventArgs e)
{
CallWS.EyrAPIEnterprise call =
new CallWS.EyrAPIEnterprise();
call.Url = "http://server/virtualdirectory/services/APIEnterprise.asp";
//If you are using the Default Credential
then set this property to true.
//call.UseDefaultCredentials =
true;
//If you are using Windows (NTLM)
authetication then set the below property.
call.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
string systemID =
string.Empty;
XmlDocument doc =
new XmlDocument();
string xmlString = ((System.Xml.XmlNode[])call.GetSystems())[0].OuterXml;
doc.LoadXml(xmlString);
textBox1.Text = xmlString;
//You can get the value from the XML
output using the XPath
}
}
}
How to consume the ASP.net Web Service
Now in .net we can have the Web Service written in ASP.net (.asmx) and use that
in ASP.net or C# (windows) application.
First we will create the Web Service using ASP.net. You can create using the VS
IDE or Simple in any text editor.
A Web Service Example
In the following example we will use ASP.NET to create a simple Web Service that
converts the temperature from Fahrenheit to Celsius, and vice versa:
public class
WebService : System.Web.Services.WebService
{
public WebService () {
//Uncomment the following line if using
designed components
//InitializeComponent();
}
[WebMethod]
public string
HelloWorld() {
return
"Hello World";
}
[WebMethod]
public int
FahrenheitToCelsius(int far)
{
return ((((far) - 32) / 9) * 5);
}
[WebMethod]
public int
CelsiusToFahrenheit(int cel)
{
return ((((cel) * 9) / 5) + 32);
}
}
This document is saved as an .asmx file. This is the ASP.NET file extension for
XML Web Services.
So when you open the Web Service from the browser using the following
http://localhost:4834/WebSite1/WebService.asmx
You will see the below list of the methods which we made as the Web Method. This
the web methods exposed by the Web Service. Now for to test, you can click on
any method for e.g. CelciusToFarheneit, it will show you the next part where you
can input the number and click on the Invoke button.
The Web service will then take that Input and convert the value from celcius to
Far as show below in the next picture.
Output
How to Call the Web Service from ASP.net Application
To access the Web Service and exposed function, I am going to use the same
approach as I did first time when accessing SOAP Web service. There are few
other ways also but just to keep in sync I am using the same one.
You have to follow the Step 1 of the first section, and the following code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace ConsumeWebService
{
public partial
class Form1 :
Form
{
public Form1()
{
InitializeComponent();
}
private void
btnShow_Click(object sender,
EventArgs e)
{
localhost.WebService call =
new localhost.WebService();
call.Url = "http://localhost:4834/WebSite1/WebService.asmx";
call.UseDefaultCredentials = true;
int far =
call.CelsiusToFahrenheit(30);
int cel =
call.FahrenheitToCelsius(86);
StringBuilder result =
new StringBuilder();
result.Append("30 Celcius = " +
far + " Fahrenheit");
result.AppendLine();
result.Append("86 Fahrenheit = "
+ cel + " Celcius");
textBox1.Text = result.ToString();
}
}
}