BizTalk Utilities CV ,   Jobs ,   Code library
 
Home Page


Add/Edit your code items
Search the code library
Browse for the code library


WCF, WS, SOAP
SOAP Client Over HTTP Using Visual C++
SOAP Toolset for VB 6.0
Remote Database Administer via XMLHTTP
Binding XML Data Island to Web Forms
Security Model for Web Services
What is a XML Web Service?


 
 

<< UncategorizedXALAN >>


By Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First Posted 04/21/2002
Times viewed 422

Temperature Conversion XML WebService


This post contains attachments
v20020421194205.zip 

Summary This article explains how to create Temperature Conversion XML WebService and how to consume the WebService by creating client Web Form and client Console application.

This article explains

how we can create the XML WebService for Temperature Conversion

how to create a proxy class

how to consume the XML WebService by creating a Client Web Form page

how to consume the XML WebService by creating a Client Console application

 

Creation of WebService

The XML WebService file should have .asmx extension.

In this file, we need to declare webservice directive as follows:

<%@ WebService Language=VB Class=TemperatureConversion>

After declaring the WebService as above, create the WebService Class as follows:

VB.NET Code

            <WebService(Namespace:=http://pandu/webservices)> _

            Public Class TemperatureConversion

                        Inherits WebService

                      

                        <WebMethod(Description:=Convert Fahrenheit Degrees to Celsius Degrees)> _

                        Public Function FahrenheitToCelsius(nFValue As Double) As Double

                                    Return 5/9 * (nFValue -32)

                        End Function

                       

                        <WebMethod(Description:=Convert Celsius Degrees to Fahrenheit Degrees)> _

                        Public Function CelsiusToFahrenheit(nCValue As Double) As Double

                                    Return (9/5 * nCValue) + 32

                        End Function

 

            End Class        

 

Here the class TemperatureConversion inherits WebService class of System.Web.Servies Namespace because it can access Application, Session and other ASP.NET objects that give information about the HTTP request that invokes the WebService.

<WebService()> attribute is used to specify XML namespace. XML Web Services use XML namespaces to uniquely identify the listener where a client sends requests.

<WebMethod()> attribute is used to expose Web Service methods as public

 

Create a Proxy class

 

After creating the WebService, we need to get the WSDL contract of WebService as follows:

 

wsdl /l:vb /o:TempConversion.vb http://masabathula/tc/Tempconversion.asmx?wsdl /n:PanduTest

 

Here wsdl.exe is the .NET utility, which can be executed at the command prompt.

/l indicates the language, /o indicates the output file, /n indicates the namespace and http://masabathula/tc/Tempconversion.asmx?wsdl is the WSDL of the asmx file.

 

This above instruction at command prompt creates a TempConversion.vb file.

 

Now compile this file as follows to get the Proxy class:

vbc /out:TempConversion.dll /t:library /r:system.web.dll,system.dll,system.xml.dll,system.web.services.dll,system.data.dll TempConve

rsion.vb

Here vbc.exe is .NET utility, which can be executed at the command prompt.

 

The above command creates a TempConversion.dll file, which is called the proxy class or the assembly class.

 

Consuming the WebService by creating a Client Web Form Page

 

To consume the WebService, TempConversion.dll should be available from bin folder of the application domain.

If there is no bin folder, create a bin folder and copy the TempConversion.dll into that folder.

Now create and write the ClientTempConv.aspx file as follows:

<%@ page language=c# debug=true %>

<%@Import Namespace=PanduTest %>

<%@Import Namespace=System %>

 

<script language=c# runat=server>

            TemperatureConversion oTemp = new TemperatureConversion();

           

            void ConvertToFahrenheit(System.Object Sender, System.EventArgs e)

            {

                        try

                        {

                                    Double nCelsius = Double.Parse(txtCelsius.Text);

                                    lblResult.Text = txtCelsius.Text + Degrees Celsius = + oTemp.CelsiusToFahrenheit(nCelsius).ToString() + Degrees Foreinheit.;

                        }

                        catch

                        {

                                    lblResult.Text = Invalid input. Please try again!;

                        }                     

            }

 

            void ConvertToCelsius(System.Object Sender, System.EventArgs e)

            {

                        try

                        {

                                    Double nFahrenheit = Double.Parse(txtFahrenheit.Text);

                                    lblResult.Text = txtFahrenheit.Text + Degrees nFahrenheit = + oTemp.FahrenheitToCelsius(nFahrenheit).ToString() + Degrees Celsius.;

                        }

                        catch

                        {

                                    lblResult.Text = Invalid input. Please try again!;

                        }                     

                                   

            }

</script>

 

<html>

            <head>

                        <title> </title>

            </head>

           

            <body>

                        <form id=frmConvert method=post runat=server>

                                    <table>

                                                <tr>

                                                            <td>

                                                                        <asp:TextBox id=txtFahrenheit text= runat=server></asp:TextBox> Degrees Fahrenheit =

                                                            </td>

                                                            <td>

                                                                        <asp:Button id=btnFahrenheit text=Convert to Celsius runat=server onClick=ConvertToCelsius ></asp:Button>

                                                            </td>

                                                </tr>

                                                <tr>

                                                            <td>

                                                                        <asp:TextBox id=txtCelsius text= runat=server></asp:TextBox> Degrees Celsius =

                                                            </td>

                                                            <td>

                                                                        <asp:Button id=btnCelsius text=Convert to Fahrenheit runat=server onClick=ConvertToFahrenheit ></asp:Button>

                                                            </td>

                                                </tr>

                                    </table>

                                               

                                    <br />

                                    <% if (IsPostBack) { %>

                                   

                                    <asp:Label id=lblResult width=100% height=25px runat=server></asp:Label>

                                   

                                    <% } %>

                        </form>

            </body>

</html>

 

Here we are required to import the Namespace PanduTest to access TemperatureConversion class.

In the above example, there are two text boxes one for Celsius Degrees and other for Fahrenheit Degrees.

There are two button controls – one to convert Celsius Degrees to Fahrenheit Degrees and the second to convert Fahrenheit Degrees to Celsius Degrees. There is one label control to display the message on user actions.

 

Consuming the WebService by creating a Client Console application

 

Create the console based ClientTempConv.vb client as follows:

  1. Import the namespaces as follows:

Imports PanduTest

Imports System

Imports System.Data

Imports System.Web.Services

Imports System.Xml

Imports Microsoft.VisualBasic

 

  1. Create a class Temperature as follows:

      Public Class Temperature

                  Protected nFahrenheit As Double

                  Protected nCelsius As Double

                  Dim oTemp As TemperatureConversion

                  Public Sub New()

                              oTemp = new TemperatureConversion()

                  End Sub

 

                  Public Function GetFahrenheit(nC As Double)

                              return(oTemp.CelsiusToFahrenheit(nC))

                  End Function

                 

                  Public Function GetCelsius(nF As Double)

                              return(oTemp.FahrenheitToCelsius(nF))

                  End Function

      End Class

            Here oTemp is instance of TempConversion of WebService.

  1. Create a class TestTemperature as follows:

            Class TestTemperature

                        Public Shared Sub Main()

                       

                                    Dim oTemp As Temperature

                                    oTemp = New Temperature()

                                   

                                    Console.WriteLine(Using VB.NET ...)

                                   

                                    Console.WriteLine(10 Degrees Celsius = & oTemp.GetFahrenheit(10) & Degrees Fehreinheit)

                                    Console.WriteLine(20 Degrees Celsius = & oTemp.GetFahrenheit(20) & Degrees Fehreinheit)

                                   

                                    Console.WriteLine(10 Degrees Fahrenheit = & oTemp.GetCelsius(10) & Degrees Celsius)

                                    Console.WriteLine(20 Degrees Fahrenheit = & oTemp.GetCelsius(20) & Degrees Celsius)

                        End Sub

            End Class 

            Here oTemp is the instance of Temperature class.

  1. Compile the ClientTempConv.vb file as follows:

vbc /r:System.dll,System.Data.dll,Microsoft.VisualBasic.dll,System.Xml.dll,System.Web.Services.dll,TempConversion.dll ClientTempConv

.vb

      This creates an executable file ClientTempConv.exe.

 

System Requirements: Microsoft.NET Framework

Additional information


Rate this article on a scale of 1 to 10 (0 votes, average 0)

Your vote :  

<< UncategorizedXALAN >>





Leave a comment for this article
Your name
Your email (optional)
Your comment
Optional: Upload an attachment
Enter the code shown:

 
 

    Email TopXML