Getting up and running with Web services isn t really all that difficult. If you re new to the topic, you ll learn all you need to get going in this quick list of bulleted points. And, if you re an old hat, well, you might just enjoy a refresher. Either way, let s explore:
Web services are at the heart of the .NET vision. They provide a standard method of allowing computers to communicate with each other.
For a Web service to work, you need two parts : a server and a client. The server is a computer containing the actual service itself. It exposes what it can do to the world through its interface. The client can discover this interface and then make certain requests. When the requests are received, the server runs the code behind the service and returns a result as appropriate.
Examples of real-life Web services include a search engine (send the service a term and it ll return a list of matching sites), a credit check service (pass the service details of an individual and it ll return a rating), a news and horoscope service (simply retrieve the latest headlines and readings ), and a dictionary (the client passes a word, the server returns a definition).
To provide an existing technology for comparison, you can think of Web services as being arguably similar to the old DCOM. However, this new way of working is based on open standards; plus it s loosely coupled , allowing for less- troublesome development. ( Remoting is another DCOM-like technology. Read the Which to Choose: Web Services vs. Remoting in Chapter 7 for more information.)
All Web service requests are done through HTTP (hypertext transfer protocol), the protocol used when browsing Web pages. This typically means that Web service requests can penetrate most firewalls.
Data returned from a Web service is passed back as pure XML (eXtensible Markup Language). This is a method of storing data through a series of tags. XML is an industry standard for storing relational data in a flat text format. It s also platform independent, and it looks a bit like HTML. But it isn t.
To create a new Web service application project in Visual Studio .NET, click on File New Project, select the Visual Basic Projects project type, and choose the ASP.NET Web service template. Specify a Web address as the project location, and then click on OK.
A new project will contain one Web service called Service1.asmx. You can add new Web services to a project by selecting Project Add Web Service from the menu. To author the code behind your Web service, right-click on the service in the Solution Explorer and select View Code.
By default, none of your methods , functions, or properties are exposed as part of your Web service. To expose a chunk of code, prefix it with the < WebMethod() > attribute, as shown here:
<WebMethod()> Public Function GetDate() As DateTime Return Now End Function
You can view your Web service in a browser by simply building the project, then visiting its dynamically generated ASMX page. Another technique is to simply press the F5 key while working on your Web service project (or selecting Debug Start from the menu).
To discover a Web service for use in your Visual Studio .NET application, select Project Add Web Reference from the menu. Specify the address of your Web service, press the Enter key, and then select Add Reference. Alternatively, browse the Microsoft UDDI Directory at http://uddi.microsoft.com/visualstudio.
To consume the functionality of a Web service in your application, simply treat it as you would any regular object. The XML is all parsed out and handled for you. For example, the following code demonstrates a discovered Web service in use:
Dim MyObject As New localhost.Service1() MessageBox.Show(MyObject.GetDate)
Your application doesn t care about the physical location of the Web service it is calling. The data could be coming from your local computer, across a network, or from the other side of the world via the Internet. So long as that connection is available, the Web service works.
Once you ve compiled your Web service, it s ready to be accessed on your machine and available to be discovered with its URL. To distribute your Web service to another server, select Project Copy Project from the menu. Alternatively, copy your ASMX, CONFIG, and VSDISCO project files across to your IIS application directory, then create a subdirectory called Bin, and copy your compiled Bin/ProjectName.DLL assembly into it.
Web service members can accept and return more than just base data types. They can take in and pass back anything, including DataSets, data-holding classes, and user -defined types (structures).
By default, when you discover a Web service, Visual Studio .NET creates a folder and namespace through which you refer to your service in code (such as localhost .Service1 or com.google.api .GoogleSearchService ). You can change this namespace by selecting your Web Reference in the Solution Explorer and changing the Folder Name property.