Other Free Encyclopedias » Online Encyclopedia » Encyclopedia - Featured Articles » Featured Articles: Timex Indiglo Watches - The History of Timex Indiglo Watches to Zermatt Switzerland Hotels - Hotels in Zermatt

Web Services Asp Net - How to Hello World with Web Services in ASP.NET

here is the code: } // end class

A web service is a general but very powerful programming model for facilitating communication on the Internet. The core concept involves combining the best elements of the Web with the best aspects of component-based development. Web services for ASP.NET succeeds where other component-based object models have failed because web services for ASP.NET do not depend on an object-model-specific protocol. It accomplishes this by communicating via SOAP (Simple Object Access Protocol) and XML (Extensible Markup Language).

In this article, we are going to introduce you to web services for ASP.NET. In order to do so, we’re going to use the classic “Hello World” concept. With it, we’ll be able to to show you how to make a web service, consume it in a simple scenario, make it available for discovery, and how to deploy it. Once you understand those four concepts, you will have the foundation needed to explore the more practical and more complex aspects of web services for ASP.NET.

The Tools Needed to Create an ASP.NET Web Service

• Basic understanding of the .NET platform
• Basic understanding of a .NET language (C# in this case)
• Basic understanding of object-oriented concepts

How to Create the Hello World Web Service for ASP.NET

Create a new web service file called HelloWorldWebService.ASMX. ASMX is the file extension for ASP.NET web services, as opposed to ASPX, which is the file extension for a standard ASP.NET file. The name of the file is not important, and it does not have to match the web service class. However, we will name it such for easy reference.

Here is the code:

<%@ WebService Language=“C#” class=“HelloWorldWebService” %>

using System;
using System.Web.Services;

public class HelloWorldWebService : WebService
{

[WebMethod]
Public string HelloWorld()
{

return “Hello World”;

}

} // end class

*The WebMethod attribute tells the compiler to expose this method as part of the web service. An unmarked public method will be available in other capacities but not as part of the service.

How to Use (Consume) the Hello World Web Service for ASP.NET

All right, now we have a very first web service. It doesn’t have a lot of real-world usefulness, but it will serve well enough for demonstration purposes. The goal in this stage is to access the web service, and display this familiar message via the method called HelloWorld().

In order to do that let us create a new ASP.NET page, HelloWorldWebServiceTest.ASPX. Next, add a button to the page. You can add it manually or through the designer. Once you’ve added it, you’ll want the form code for the button to look something like this:

<form action=‘http://localhost/HelloWorldService/HelloWorldWebService.cs.asmx/HelloWorld’ method="GET">

</form>

Now you can invoke your web method by navigating to the appropriate URL for HelloWorldWebServiceTest. Press the button, and the web service will return the result to you. You may be surprised to find that the result is a complete XML document rather than simply the words ‘hello world’. This is to be expected. Remember that earlier we mentioned that web services use XML or SOAP to communicate. In this example, we simply allow the XML document to display in the browser. In a more advanced scenario, we would extract the value from the XML document and use it in a useful way, such as displaying the words on a label.

How to Create a Discovery File

In the example above, the web service existed locally with the webpage that wanted to use it. However, in real world scenarios, a web client will locate and interrogate web services, and then consume them. In order to facilitate this discovery, we need a discovery file for the web service. The discovery file is an XML document with a .DISCO extension, and it generally exists in the directory along with the web service. Here is the XML text:

<?xml version=“1.0” ?>
<disco:discovery xmlns:disco=“http://schemas.xmlsoap.org/disco/”>
/> <scl:contractRef ref=“
http://localhost/work/aspx/HelloWorldWebService.asmx? class="caps">SDL”/>
</disco:discovery>

How to Deploy a Web Service

Deploying a web service is very simple, and it is just a matter of copying the necessary files over to the appropriate directory on the server. In this case, it will be three files: the .ASMX file, the .ASMX.CS file, and the .DISCO file. The .DISCO file will allow the environment to expose the web service without compiling it. Then, just like any other ASP.NET component, the environment compiles the component only when a client requests access to it.

Web Site Hosting Reseller - What is a Web Site Hosting Reseller? [next] [back] Web Page Templates Free - 3 Design Considerations for Finding the Right Web Page Templates Free

User Comments

Your email address will be altered so spam harvesting bots can't read it easily.
Hide my email completely instead?

Cancel or