Using Web Services: How to Create

This post will go into how to create a web service using VS.

 

The first thing that needs to be done is create a web site and choose ASP.NET Web Service from the Template window.  If you are thinking about using the web service as it is intended for, reusability, then you will want to place it in a directory folder that will be accessible to all application that can use it on the intranet (although you can reference a web service by using a web address, we will not go into that). An example of a folder would be ‘WebServices’. This folder would hold all web service created. We will name this web service ‘getAllStationInfo’.

 

Once you have your web site started you will see that the default page opens up and is ready for coding.  It has already imported the namespaces needed at the top and has a public class named Service.  We will be adding more namespaces to ours because of the nature of the service – database query.  It will look like the below:

 

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Data.SqlClient

Imports System.Data

 

It also has its first function called HelloWorld() for editing. All we are going to do this time is create a web method that can be consumed. Here is what it looks like at first:

 

<WebMethod()> _

    Public Function HelloWorld() As String

        Return “Hello World”

    End Function

 

This is what a finished one looks like:

 

<WebMethod()> _ 

    Public Function getAllStationInfo() As DataSet

        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings(“DatabaseNameHere”).ConnectionString)

        conn.Open()

        Dim adapter As New SqlDataAdapter(“Select statement”, conn)

        Dim ds As New DataSet()

        adapter.Fill(ds, “name of function here”)

        conn.Close()

        Return ds

    End Function

 

You will notice that the SqlConnection is referencing a named connection string:

 

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings(“DatabaseNameHere”).ConnectionString)

 

This is referencing code from the web.config file.  The connection string there looks like so:

 

……<configuration>

            <appSettings/>

            <connectionStrings>

                        <add name=databaseName connectionString=server=Servername’; Database=databaseName; uid=; pwd=; providerName=System.Data.SqlClient/>

            </connectionStrings>

            <system.web>……

 

Now to get back to the web service. You will notice that right before the function there is a line: <WebMethod()> _

This attribute enables the web service method to communicate with we appications located at local or remote locations.

 

Something I failed to mention previously is that there is a web service attribute that can be set. The syntax is like so:

 

<WebService(Namespace:=“http://servername”)> _

 

This will provide additional information about the webservices, such as information about the functionality of the web service itself.

 

All we are doing in this web service is creating a dataset to be consumed by anyone who wants to use it.

 

To test the web service, all you have to do is right click on the service.asmx file and view in browser. If it works you will see the function name that you can click and then the next page allows you to invoke the function. The reuslt of clicking on invoke is a new page filled with XML data. If you see this, you have accomplished creatign a web service.  You can create a web service to do anything you want it to do.  Just keep in mind that the key is reusablity.

 

In the next post, we will look at how to actually consume a web service in an appication and how to display the results of a web service.