Many of the technologies that support XML Web Services, like XML, are not new. What is new is the ease with which you can consume and implement Web Services. So far you know that Visual Studio .NET seamlessly employs UDDI, DISCO, and WSDL to incorporate XML Web Services into client applications. Creating Web Services is even easier. In a nutshell , you create an XML Web Service project by selecting the ASP.NET Web Service applet from the New Project dialog (Figure 13.3). After you have created the project, all you have to do is define methods and types and apply the WebMethod attribute to public methods you want to export. Listing 13.4 shows the complete (albeit short) listing for the Web Service containing the GetCommission Web method.
Listing 13.4 A Complete Web Service Using the GetCommission Web Method
1: Imports System.Web.Services 2: 3: <WebService(Namespace := "http://tempuri.org/")> _ 4: Public Class Service1 5: Inherits System.Web.Services.WebService 6: 7: [ Web Services Designer generated code ] 8: 9: <WebMethod()> _ 10: Public Function GetCommission(ByVal ID As Long) As Commission 11: Return New Commission(1000, 0.075) 12: End Function 13: 14: End Class
Figure 13.3. The New Project dialog creates a new ASP.NET Web application from a project template.
The WebService attribute and Inherits System.Web.Services.WebService statements indicate that Service1 is a Web Service. You can add all the supporting code you want, but only public methods tagged with the WebMethod attribute (line 9) will be represented by proxy methods when you import the Web Service. Everything else about the Web Service class is consistent with any class.
Applying the WebService Attribute
The WebService attribute is applied to a Web Service class automatically by the project template. The default Namespace named argument is tempuri.org . Before you deploy the Web Service you should modify the Namespace value to point to your company's URL. For example, I would modify my Web Services as follows : <WebService(Namespace := "http://www.softconcepts.com/")> . This uniquely identifies your Web Services even if other vendors define Web Services with similar types and methods. You can also specify Name and Description named arguments for the WebService attribute. These values will show up in the description page, for example, when someone browses to the Web Service in the Add Web Reference dialog.
Writing a Web Method
To implement a Web method, define a public method and apply the WebMethod attribute. There are few limitations to the kind of information you can pass to and return from a Web method. You can pass and return simple data types to XML Web Services. You can also return complex types from Web methods, such as the Commission type demonstrated in Commissions.sln . And because XML is an integral part of ADO.NET and the ADO.NET DataSet is serializable, you can return an ADO.NET DataSet from a Web method. For more examples of returning complex data types and DataSet objects from Web Services, refer to Chapter 14.