How to create a Web service using Visual

Creating a Web service in Visual Studio .NET is very simple. ASP.Net server side technology allows developers to create web services and the .NET framework provides a sophisticated set of tools and code to consume web services.

In this exercise, we will build a simple web service ‘Hello World'. Most articles teach about web services using the Hello World example. But few explain the technology behind this web services example in detail. In this article we will learn both.

Let us start by creating the Hello world web service. We will create the web service using and visual basic language. The IDE we will use is Visual Studio. Net. We can also create a ASP.NET Web Service in C# programming language using visual studio. Net IDE.

create new web site dialog box

To create the web service, open Visual Studio and select File-> New Web site from the menu. Make sure Visual Basic is selected in the language box and HTTP in the location box. Select ASP.NET Web service from the template list. Enter the name as http://localhost/HelloService as shown in above figure and click OK.

Visual Studio 2005 will create a new web service project. A new virtual directory - HelloService - will be created on your computer's Web server. This action also creates two files called Service.asmx and service.vb. Please note that Visual Studio creates the entry point file with .asmx extension and the other code behind file with .vb extension. These files are automatically added to the project, which we can see in a solution explorer.

The service.asmx and service.vb files represent a single web service. WebServices do not have a visible interface and so, the design interface is empty. When we open the Service.asmx file, only the following code is seen.

The service.asmx file serves as the entry point into the web service.

The other file, service.vb, is the code-behind file for the web service. We can write the functionality of the web service in this file. The code-behind file imports the System.Web.Services namespace. This namespace contains the classes that are required to build and use Web services. We can view this file by switching to the code view for the service.asmx file.

Note that a single project can contain multiple WebServices, each web service implemented by a different class.

If we open the service.vb code-behind page, we can see the default code which is created by vs.NET automatically, as listed below.

code which appears when the service.vb file is opened

From the above code, we see that service is the class that contains all the code that implements the web service methods. This is actually the name of the WebService; it's named after the class and not after the project. The .vb and .asmx files together form the Code model for the web services.

As we have already seen a web service is basically a class that sits on the server. Building a Web Service with Visual Studio is as simple as writing a class. The class inherits from System. Web. Services, and this base class includes all that is necessary to make the functions (methods) of the class available on the Web. The Web Service class belongs to the system.Web.Services namespace. This class provides direct access to built-in ASP.NET objects, such as Application and Session. If an application does not require these objects, we can create a Web service without deriving it from the Web Service class. So, Note that deriving from a web service base class is optional.

Examining the Web Service Files

Service.asmx file

web service directive code

In the above line of code, @ WebService is the web processing directive that specifies a class name, here service for a web service and the language used to create the web service is visual basic.

Service.vb file

Explanation of the WebService attribute

The WebService attribute is an optional attribute that can be used to provide additional information about the Web service. We can also use the WebService attribute to provide a unique namespace for the web service. This namespace can be used by client applications to resolve conflicts that may arise due to different Web services having methods with the same name. The Webservice attribute can be provided before the Web service class declarations as shown below.

the web service attribute code which appears before the web service declations

The description of the Web service appears in the browser window when we try to access the web service directly by providing the path.

The WebMethod Attribute

The methods that we want to expose to web service are marked with Webmethod attribute.

The Webmethod attribute enables us to call remotely, the web service methods over the internet. The syntax for applying the Webmethod attribute to a method is given below.

syntax for applying the Webmethod attribute to a method is given below.

code syntax for applying the Webmethod attribute

The methods of this class don't return an HTML page, instead, they return one or more values, packaged as XML documents.

Basically, if you can write a function, you can write a web method; it's that simple. The difference between a class and a WebService class, is that the members of the WebService can be called remotely.

VB.NET uses less than and greater than symbols to the <WebMethod> attribute to appear on the line before the function definition.

From the above code, we note that class name is Service and method is HelloWorld.

If we want to change its name, we must rename the Service item in the Solution Explorer's window, as well as the name of the class in the code.

Note that we can also create methods without preceding them with the WebMethod attribute. However, these methods cannot be used to expose some functionalities to applications. They can only be used by the other methods within the Web service class. We can also write web services are as plain text files with an extension of .asmx. We can use notepad to develop the web service. The code is listed below. We will use this code example in our articles on deployment and consuming a web service.

web service method example code

After adding code to a notepad file, save the file with the extension .asmx.

cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »