Creating Our Own Web Service


Wouldn't it be cool if fans could display tour diary entries on their own websites? Well, with web services, they can. Let's make a web service available from our website that fans can tap into. We'll provide one method to return all tour diary entries.

Try It Out—Creating a Tour Diary Web Service

As usual, Web Matrix makes the process of providing a web service simpler through the use of a template file. We'll use this template to provide our web service, and then customize it.

  1. Using the Web Services | Simple template, create a new file called TourDiary.asmx (asmx is the extension for web services). Use the namespace CAM and the class TourDiaryWS for this file:

    click to expand

  2. The template file that is created now has an example method that adds two integers together and returns the result. Remove this and add the ReadTourDiaryEntries() method below so that the file contains the following:

    <%@ WebService language="VB"  %> Imports System Imports System.Web.Services Imports System.Xml.Serialization Imports System.Data Imports CAM Public Class TourDiaryWS  <WebMethod> Public Function ReadTourDiaryEntries( _  ByVal SortValue As String) As DataSet  Dim TDDB As New TourDiaryDB()  Return TDDB.ReadTourDiaryEntries(SortValue)  End Function End Class

That's it – we now have a completed web service.

ASP.NET provides an easy way to test your web service. You can browse to your .asmx page in the usual way as you have seen throughout this book, and see the methods provided, like the following:

click to expand

If you click on the ReadTourDiaryEntries link, you can even invoke the method (of course, this is only for testing – you normally access these services from websites or other applications). The page will now prompt you to provide values for the parameters:

click to expand

If we invoke the method with SortValue set to Author, we'll get something like the following XML returned, depending on what is in the tour diary of course:

click to expand

This is the XML that comprises the returned dataset. In the next Try It Out, we'll use it in a fan's web page, where their ASP.NET code will call and utilize the returned data from this web service. Firstly, though, let's look at the code to see what is going on.

How It Works

The WebService directive declares this file as providing web service functionality. You must also specify the language within the page, and the class that will be providing the web services. In this case, the class within our page named TourDiaryWS is providing the functionality:

<%@ WebService language="VB"  %>

We utilize several namespaces to provide web service functionality. The first three that are placed in the code by Web Matrix are always required for a web service. The two that we add are needed because we're providing functionality that returns datasets, and we use functionality within the CAM namespace to provide the data:

Imports System.Data Imports CAM

Methods within a class providing web services are only available to the web service if they are marked with the <WebMethod> attribute. You should only enable the functions you explicitly want available to the users of your service. Your application will be more secure this way:

  <WebMethod> Public Function ReadTourDiaryEntries( _                 ByVal SortValue As String) As DataSet

We're using our TourDiaryDB class to provide a dataset containing all of our tour diary entries, sorted by the value contained in the SortValue property:

    Dim TDDB As New TourDiaryDB()     Return TDDB.ReadTourDiaryEntries(SortValue)   End Function

Now that we've created our web service and tested it, let's actually use it, or to put it another way, consume it.

Try It Out—Consuming our Web Service
  1. Create a new directory called Fan. Then, in Web Matrix, choose Tools | WebService Proxy Generator.

  2. Enter the following parameters in the proxy generator dialog:

    click to expand

    Hit Generate to have Web Matrix generate the proxy.

  3. If the above works, you should have two files created. The first, CAMTourDiary.vb, contains a proxy to our ReadTourDiaryEntries() method. The second, bin\CAMTourDiary.dll, is the compiled assembly.

  4. Now create a fan page in the Fan directory using the Data Pages | Simple Data Report template and call it CornflakeDiary.aspx.

  5. Change the Page_Load method to match the following:

    Sub Page_Load(Sender As Object, E As EventArgs)  Dim TDWS As New FanSite.TourDiaryWS()  DataGrid1.DataSource = TDWS.ReadTourDiaryEntries("PostedDate")  DataGrid1.DataBind() End Sub

This works by creating an instance of our generated proxy from Step 3 above. We used the namespace FanSite, and the web service class we created a proxy for was called TourDiaryWS. We call the ReadTourDiaryEntries() method to set the data source for the grid on our page.

Of course, to finish up the page, you should encode the returned data for display as we've done with the other examples in this chapter and the previous few chapters.

I have the band's website running on my workstation on port 8080, so I'm going to run the fan site on port 8081. Therefore, we need to manually start another instance of the Web Matrix web server. In order to do this, we have supplied a batch file in the code download that you can copy to your system and then run by double-clicking. Alternatively, you can create one yourself by creating a new text file and entering the following code:

 cd c:\Program Files\Microsoft ASP.NET Web Matrix\v0.5.464 webserver /port:8081 /path:c:\BegWebMatrix\Chapter16\Fan pause 

Save the file as StartWebServer.bat, then double-click it to run the commands. This starts a new instance of the web server listening on port 8081 and serves the content in the Fan directory.

Now load http://localhost:8081/CornflakeDiary.aspx and you should get the following result:

click to expand

We're done! We've just consumed our own web service. Now our fans can provide our tour diary entries on their own websites. We didn't have to know any XML, SOAP, or any of the other complex technologies to make it happen.




Beginning Dynamic Websites with ASP. NET Web Matrix
Beginning Dynamic Websites: with ASP.NET Web Matrix (Programmer to Programmer)
ISBN: 0764543741
EAN: 2147483647
Year: 2003
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net