Testing a Web Service with a Browser

333 Server This is an object that holds information about the web server. See Chapt er 6 for more information. Session This is an object that can be used to hold session state. See Chapt er 6 for more information. User If Windows authentication is used to authenticate the web-service client, the User property references an object containing information about the user. The type of this property is IPrincipal.

7.2 Testing a Web Service with a Browser

Web services deployed on ASP.NET can easily be tested with a web browser, because the ASP.NET framework itself includes the ability to generate browser screens based on the definition of the web service. For example, browsing to the .asmx file shown in Ex am ple 7- 1 using the URL http:localhostWebServicesHelloWebService.asmx produces the browser screen shown in Figur e 7- 1 . Figure 7-1. Browsing to a web service hosted in ASP.NET To test a web service in a web browser, the .asmx file must be deployed in an IIS virtual directory. Using the browser to view the .asmx file directly from disk doesnt work. The HelloWebService in large font in Figur e 7- 1 is the name of the web service exposed at the given URL. This is the name of the implementation class or the value given by the Name parameter of the WebService attribute, if present. Underneath the main heading there are two additional links. The first, marked Service Description, links to the WSDL description of the service. Web-service descriptions and WSDL are explained in the next section. The second, at the bottom of the test window, is the list of web methods exposed by the web service. In the case of HelloWebService, there is only a single method, SayHello. Clicking on the name of the method brings up a screen with information about that method and with a mechanism for testing it. The screen is shown in Figur e 7- 2 . Figure 7-2. The test screen for the SayHello web method 334 Near the top of the test screen for the SayHello web method is a place to enter a value for the methods single parameter, Name , and a button that invokes the method. At the bottom of the screen is a list of sample invocations and responses for the web method, in the protocols that ASP.NET understands. ASP.NET web services can be invoked using the SOAP, HTTP GET, or HTTP POST protocols. When youre developing clients on the .NET platform, none of this matters to you—.NET does the work of wrapping invocations in the appropriate SOAP messages. However, when youre calling a web service from a platform that doesnt have native support for writing web-service clients, this information can be very helpful. Additionally, the HTTP GET and HTTP POST protocols may be simpler to implement on platforms that dont provide SOAP support. These protocols wont be discussed further, however, because this book is about developing on the .NET platform. Clicking the Invoke button on the screen shown in Figur e 7- 2 invokes the web service using the HTTP GET protocol. The response is an XML document that encodes the result of the web-method invocation see Figur e 7- 3 . This is not a SOAP response. Rather, it is in the format used when responding to HTTP GET or HTTP POST invocations. It is sufficiently readable to see that the web method is performing as expected. Figure 7-3. The result of clicking the Invoke button in Figu r e 7 - 2 335

7.3 Web-Service Descriptions