Get ready for the session variable user object!

http://msdn.microsoft.com/en-us/library/aa479313.aspx 

Here is an interesting article, “How to Share Session State Between Classic ASP and ASP.NET”

May be important when we start consuming the web service.

Using Web Services: How to Consume

Consuming a web service is easier that creating one. All you have to do is create a reference and then add a couple of lines of code and you have the web service to use in your application.

 

The way to add a web reference to your web service is to first figure out where the web service resides.  It can be a full web address or it can be a local folder.  Once you figure that out you need to create a reference to it. To do this:

 

Right click on the project and select add web reference. When you do this you are presented with a box – Add Web Reference. From here you will choose the correct link under Browse to: and select the web service you will use.  I chose local machine since I am working within an Intranet.  The service URL shows up. I just have to click it and the URL at the top auto fills with the URL of the web service. Then you choose add reference under the right window to add the reference to your application.  Visual Studio will ask you if you want to put it in its own folder or not. I like to keep my things separate so I choose to keep them separate. 

 

Once you do that, all you have to do is add one of code to start using it in your application:

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim wcConvert As New foldername.Servicename()

 

When you create the web service it will be in a folder called App_WebReferences. Under that will be the folder name mentioned above. Under the folder is the service file. So you want to reference it by foldername.servicename().  That is it! You can start using it now.

 

What I did was convert it to a DataView so I could extract the data out of the dataset I needed. In order to do this, it woul dbe best to convert the dataset (web service) to a dataview. You have more options with a dataview.  I found out through trial and error that you cannot set up a dataview as a web service.  It is not a recognized XML format apparently.  So to convert you need to add this to your code:

 

Imports System.Data

Imports System.Data.DataView

Dim dv As New DataView

dv = wcConvert.functionname().Tables(0).DefaultView

 

With the dataview you can now sort (dv.sort) and select distinct values DDLcontrolname.DataSource = dv.ToTable(True, “columnName”).

 

With all the above in place you can now use the DataSource to bind to a control.

 

DDLcontrolname.DataTextField = “Name”

DDLcontrolname.DataValueField = “Name”

DDLcontrolname.DataBind() 

That is all there is to it. Now I have to figure out how to implament caching so not to hit the web service every page refresh. Until next time, always remember, with great power comes great responsibility.

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.

Web Service Request:::::::::::::::::

The webservice could also include mission information.

Aton,Defense Readiness, etc.,etc.

Methods would include:

getAllMissions(), getMissionFullName(‘Aton’), getAcronym()

That would be a nice addition to the service.

Using Web Services: Best Practices

  • Ensure that the connection between web service and web server is asynchronous. This means that the web application does not need to stop its processes until the web server responds to its request.

  • Minimize the number of requests that an application needs to send to a web server to consume a web service. Send as much information as can be sent. This will ensure network traffic is cut to a minimum by decreasing the number of requests.

  • Ensure the web service is created using the concepts of object oriented programming. Create once use many times. This also should create a standard of information used.

  • Minimize time of response by using cache. This will ensure that every time the web service is called upon, the web service is not run, the cache will be used. You can set the time in which the cache can be refreshed so to catch any updates that may happen.