Welcome To My Blog

This blog contains posts that may benefit ordinary visitors or programmers (particularly .NET programmer).
To view posts related to particular subjects, click the link under Labels.

Monday, August 10, 2009

Using Web Service in ASP.NET

A Web Service (also Webservice) is defined by the W3C (World Wide Web Consortium) as "a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface described in a machine-processable format (specifically WSDL - Web Service Description Language). Other systems interact with the Web service in a manner prescribed by its description using SOAP (Simple Object Access Protocol)-messages, typically conveyed using HTTP (Hyper Text Transfer Protocol) with an XML (Extended Markup Language) serialization in conjunction with other Web-related standards."


Please follow this link to get additional information about the web service from Wikipidia.


What is a web service in the context of ASP.NET? A web service can be considered as a remote software component. In this case, the component that resides in a hosting server offers functionalities that can be consumed by any application remotely via the internet.



CREATING A WEB SERVICE FILE


In ASP.NET, we can create a web service file with the extension ".asmx". We can use any of the .NET supported languages to write the codes. However, it is recommended to use C# programming language due to its efficiency and strong type support.

The picture below shows an example of a web service file.
As you can see from the picture, the web service file (asmx) has a page directive such as

<% @ WebService Language="VB" class="TestWS" %>

which describes the language used in the page and name of the class. In this case the language is VB.Net and the class name is TestWS.

Every method that you want to expose as a web method must be declared with the webmethod attribute and public access modifier. Once a method is declared as a web method, it can be called remotely by any other application via the web/internet.

Since the web service is using HTTP, it must be placed in a web application folder of the web server. This works almost similar to the ordinary web application.


CONSUMING THE WEB SERVICE

To consume a web method in the web service file, we must first create a proxy file in our machine using the WSDL tool that comes with Visual Studio. The following is an example how to use the WSDL command-line tool to create the proxy file (note that D:\webservice> is the DOS command prompt where you type the command -- D:\ is the drive and webservice is the folder name),

D:\webservice>wsdl http://www.wakiltakaful.com/mywebservice/testws_vb.asmx
/language:vb /namespace:safuan

In this case, the default output file that is created will be named TestWS.vb and the content is as shown as in the picture below.

Now we can use the VB command-line compiler to compile the proxy file to an assembly as shown below:
D:\webservice>vbc testws.vb /target:library
The output file will be testws.dll.

Once you have the assembly, you may make reference to it in your winform or webform application. If you use Visual Studio, the assembly will be copied locally in the bin folder of the application folder.

The picture below shows how the proxy is added to a web application folder.
And the picture below shows the VB code to use the proxy.
And finally the picture below shows how the web service works!
Remember that the assembly only contains the definition not the actual program logics. The program logics is in the web service file located in the remote server. In other words, you still must have a working internet in order to use the web methods.

Tuesday, August 4, 2009

Opening A Web Application From VS2005


Welcome back... In this article I would like to tell how to open a web application that you have copied from somebody else from Visual Studio 2005. I am assuming that you have a running Internet Information Services (IIS) in your system.

Simply follow the steps below:
  1. Copy the folder containing the web application to the \Inetpub\wwwroot folder. (This is the default folder for IIS.)Note: Actually, you may copy the application folder to any folder in any drive but it will be easier if you copy it to the default folder used by IIS.
  2. Open IIS. In IIS, expand the Default website folder until you see the application folder you just copied. (It may appear with yellow folder icon.) Right click the folder name and choose Properties from the context menu that appears. Click the Create button from the dialog box followed by the OK button.

    The yellow folder icon changed to an opened package icon as shown below.
  3. Open Visual Studio 2005. In VS2005, open the File menu and choose Open Web Site. In the Open web site dialog box, click the Local IIS tab and select the web site you want to open followed by the Open button.