Creating the Client with VS2005

The easiest way to create a web service client is to let VS2005 do all the work. This includes creating the proxy for the web service and adding a reference to that proxy to the client application. You will see how to do some or all of the steps outside VS2005 later in this chapter.

Why wouldn’t you let VS2005 do all the work, you might ask? By taking control of the process and performing certain steps manually, you gain flexibility and features that may be helpful under certain (admittedly unusual) scenarios. In addition, you gain a more thorough understanding of how web services work and what is happening behind the scenes, so doing it yourself has great geek appeal and impresses your friends at parties.

To start, first create an ultra-simple web service in VS2005. Call it WebServiceSimple. This will bring up the default start-up boilerplate code in the code-behind file with a web method called HelloWorld. Modify this web method a bit to return the current time stamp, as shown highlighted in Example 16-1.

Example 16-1. App_Code\Service.cs for WebServiceSimple

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World. The time is " +
            DateTime.Now.ToLongTimeString(); } ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.