BizTalk Utilities CV ,   Jobs ,   Code library  
 
 

 

  

Creating Web Services in ASP.NET

What are Web Services?

To understand the meaning of term "Web Services" we will take an analogous example from the world of COM. If you are programmer working with Microsoft technologies, you have probably used third party components or at least components developed for your own applications. The COM components you developed basically provide "services" to your application. For example a component developed for a banking application might be providing services such as loan calculation, interest calculation etc.

If you needed the same business logic in a distributed way (in many places around the web) this component will be of great use to you. Components also isolate your business logic from the rest of the application.

Web services offer similar functionality. Web services offer "services" over the "web". The communication between your ASP.NET pages and web service is via HTTP protocol using SOAP standards (Simple Object Access Protocol).

In simple terms it works as follows:

  • Your ASP.NET page requests a service from a Web Service component
  • Your request is converted into SOAP format (which is in XML)
  • The web service processes your request and sends back the result again in an XML format over SOAP

Fortunately, as an ASP.NET developer you are isolated from the pain of SOAP internals.  All of that is handled for you!

The following points need to be noted about web services:

  • Web services are written as plain text files with an extension of .asmx
  • You then convert this .asmx file first into VB or C# source code using a utility provided with ASP.NET called WebServiceUtil.exe
  • You then compile the source code into a DLL file which makes the "Web Service" component

Service Description Language

Service description language or SDL is nothing but an XML document describing the web service. It is analogous to Interface Definition Language (IDL) or Type Libraries(TLB). It gives details like service method names, their parameter data types, return values etc.

Creating Your First Web Service

In this section we will create a simple web service called Greeting Service. This web service will provide various greeting messages. You will need to pass an integer indication the type of message you want e.g. Birthday Greetings, New Year Greetings etc. Following are the steps involved in creating a web service:

  • Create .asmx file containing source code for the service ( GreetingService.asmx in our case)
  • Convert .asmx file into VB or C# source code file
  • Compile VB or C# source code thus formed
  • Deploy the resulting DLL to the applications bin folder

Create GreetingService.asmx file containing source code for the service

<%@ webservice language="VB" class="greetings"%>

imports system.web.services

public Class greetings

       public function <WebMethod()>getMsg(id as integer)as string

              select case id

              case 101 

                  getMsg="Happy Birthday"

              case 102

                  getMsg="Happy New Year"

              case else

                  getMsg="Seasons Greetings"

              end select

       end function

end class   

Let us dissect the code:

  • Webservice source code is stored in simple text files having extension .asmx.  In our case we created a file called GreetingService.asmx
  • <%@ webservice language="VB" class="greetings"%>
    The webservice directive indicates that this is a web service. Language attribute specifies the language used to write the web service. We have used VB in our example. The class attribute specifies the class name that constitutes the web service. One .asmx file can have multiple class files (some of which may be supporting classes). Out of available classes the class specified in the class attribute is used for creating the web service.
  • public function <WebMethod()>getMsg(id as integer)as string
    This line marks getMsg() method as the "web callable" method. This method returns the greeting message based on id we pass

Convert .asmx file into VB or C# source code file

Now that we have our web service source code file ready, we need to convert it into VB source code. To do this ASP.NET comes with a utility called WebServiceUtil.exe. This utility is used for other purposes as well but we will consider its use only to generate VB source code. Type in the following command at the DOS prompt:

WebServiceUtil /c:proxy /pa:http://localhost/mywebapp/GreetingService.asmx?sdl /l:VB /n:GreetingService

Here,

  • /c:proxy represents that we want to create proxy for the client
  • /pa:http://localhost/mywebapp/GreetingService.asmx?sdl We need to supply the SDL of the web service. This can be achieved by appending SDL to the path of our .asmx file. You can even invoke this URL in browser and view the resulting XML code.
  • /l:VB Specifies that the resulting code should be generated in VB
  • /n:GreetingService Indicates that name for the resulting namespace should be GreetingService

After executing above command you will get a file called greetings.vb and it contains something like this :

'--------------------------------------------------------

' <autogenerated>

'     This class was generated by a tool.

'     Runtime Version: 2000.14.1812.10

'

'     Changes to this file may cause incorrect

'     behavior and will be lost if

'     the code is regenerated.

' </autogenerated>

'------------------------------------------------------

Imports System.Xml.Serialization

Imports System.Web.Services.Protocols

Imports System.Web.Services

Namespace GreetingService

    Public Class Greetings

        Inherits System.Web.Services.Protocols.SoapClientProtocol

        Public Sub New()

            MyBase.New

            Me.Path = "http://localhost/BAJSamples/ASPPlus/WebServices/GreetingService.asmx"

        End Sub

        Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/getMsg", RequestElementName:="getMsg", ResponseElementName:="getMsgResult")> GetMsg(ByVal id As Integer) As String

            Dim results As Object() = Me.Invoke("GetMsg", New Object() {id})

            return CType(results(0),String)

        End Function

        Public Function BeginGetMsg(ByVal id As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult

            return Me.BeginInvoke("GetMsg", New Object() {id}, callback, asyncState)

        End Function

        Public Function EndGetMsg(ByVal asyncResult As System.IAsyncResult) As String

            Dim results As Object() = Me.EndInvoke(asyncResult)

            return CType(results(0),String)

        End Function

    End Class

End Namespace   

Do not bother about the code since you will not be generally modifying it manually.

Compile VB or C# source code

Now we will compile the VB source code to get the final DLL.

Type in following command

vbc

/out:GreetingService.dll

/t:library

/r:System.Xml.Serialization.dll

/r:System.Web.Services.dll Greetings.vb

pause

If you are interested in what each of above switch means just type vbc at the command prompt and find out yourself :-)

You finally got the GreetingService.DLL!  Well done!  What next?

Deploy the resulting DLL

In order to use the DLL you created you must copy it in the \bin folder of your web application.  If such folder is already not there you need to create it.

Testing our service

You can now test the web service you created by using it in an ASP.NET page. You might use following code:

<%@ language="VB"%>

<script runat=server>

public sub showmsg(s as Object,e as EventArgs)

       dim c as GreetingService.Greetings

       c=new GreetingService.Greetings()

       label1.text=c.getMsg(101)

end sub

</script>

<form runat=server>

<asp:label id=label1 runat=server text="no msg set yet"/>

<asp:button id=btn1 runat=server onclick="showmsg" text="Click" />

</form>   

You can create instances of the web service component just like any other object.
Since the DLL file is already in the bin folder, our service is already available to the ASP.NET page

Summary

Web Services provide "web callable" functions that you can call from your .NET applications. Web services transfer data using SOAP. The details about various web methods are provided by the SDL. To use a web service you create a proxy for the web service and call the methods on the proxy from your .NET application.

Please feel free to send your feedback and suggestions at bipinjoshi@yahoo.com or visit his website at http://www.bipinjoshi.com

 

  
 

Recent Jobs

A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En
A Greate Opportunituy as a SQL Deve
An immediate job opportunity as a B

View all Jobs (Add yours)
View all CV (Add yours)



Information Online

swimming pool contractor
chicago web site design
help desk
Domain Names
help desk services
Dolce&gabbana sunglasses
answering service


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP