| Introduction .NET has made it easy to write and publish Web services. Just write an .asmx file with one or more public methods, decorate it with a 'WebMethod' attribute, and you are in business. When you load the Web service's .asmx page in a browser, you will see a very helpful documentation page that is automatically generated by ASP.NET. This article shows how to customize this documentation page and hide the associated WSDL file. 
Figure: Our simple Web service Some Background ASP.NET actually generates this page by picking up a default page template for Web services, which ships with the .NET Framework. You can find this default template in <Windows System Folder>\Microsoft.NET\Framework\<.NET Framework Version>\CONFIG\DefaultWsdlHelpGenerator.aspx. This template page uses reflection to pick up information about the Web service. ASP.NET uses this template page and generates the HTML and WSDL parts of the documentation. You can access the WSDL file for the Web service by appending the "WSDL" query string parameter to the .asmx file URL e.g. http://localhost/HelloWorldService.asmx?WSDL. 
Figure: WSDL for our simple Web service Hide Web Service Documentation This WSDL provides all the information about the Web service. Anyone can consume this WSDL and generate a proxy to talk to the Web service. Now say that you are working on a secure Web service and you would like to hide all the information about the Web service. It is only available to some known clients, which are already aware of the WSDL/schema. You can instruct the ASP.NET runtime not to generate any documentation for the Web service by updating the web.config like this: <?xmlversion="1.0"encoding="utf-8"?> <configuration> <system.web> <!-- HIDE
THE DOCUMENTATION FOR THE WEB SERVICE --> <webServices> <protocols> <removename="Documentation"/> </protocols> </webServices> </system.web> </configuration> As soon as you update the web.config, you can neither access the HTML documentation page nor the WSDL for the Web service. 
Figure: Web service without HTML documentation 
Figure: Web Service without WSDL Document Customizing the HTML Documentation Page So now you know how to get rid of the documentation pages. But what if you only want to remove the WSDL part of documentation and keep the HTML documentation page to give a face to the Web service? Comment out the changes in web.config for now. You will come back to removing the WSDL later. Now is the time to focus on customizing the HTML documentation page. You can alter the default page template (DefaultWsdlHelpGenerator.aspx) that ASP.NET uses to generate the default documentation page, but this will affect all the Web services deployed on the Web server. A better choice would be to write your own page. So now create a very simple ASP.NET page called helpPage.aspx and include it in the same solution. 
Figure: Customized help page To hook up the custom help ASP.NET page to the Web service, you have to update your web.config file: <?xmlversion="1.0"encoding="utf-8"?> <configuration> <system.web> <!-- HOOK
UP OUR CUSTOM HELP PAGE WITH OUR WEB SERVICE --> <webServices> <wsdlHelpGeneratorhref="helpPage.aspx"/> </webServices> </system.web> </configuration> After modifying the web.config, navigate to the Web service page. You should see the customized help page instead of the default ASP.NET page. 
Figure: Custom page in action Now back to switching off the WSDL documentation only. Frankly there is no elegant way to turn off WSDL and leave the HTML documentation alone. So you can simply forbid ASP.NET to serve WSDL pages by trapping the Application_BeginRequest event in the global.acax file. protectedvoidApplication_BeginRequest(Objectsender, EventArgse) { string requestPath = Request.RawUrl.Trim().ToLower(); if (requestPath.IndexOf("?wsdl")
> 0 || requestPath.IndexOf("?disco")
> 0) thrownewHttpException(404, string.Empty); } Now when you try to navigate to the WSDL page you will receive an HTTP 404 error. Selectively Turning on WSDL This will achieve our goal in production environment, but in case you want to turn on the WSDL file in development environment, you can easily implement any kind of security mechanism to selectively allow access to the WSDL file. For instance you can allow the user to see the WSDL file if he/she is running on the same machine by modifying the code: protectedvoidApplication_BeginRequest(Objectsender, EventArgse) { string requestPath = Request.RawUrl.Trim().ToLower(); if (!IsLocalRequest()) { if (requestPath.IndexOf("?wsdl")
> 0 || requestPath.IndexOf("?disco")
> 0) thrownewHttpException(404, string.Empty); } } Conclusion In this article you learned how to customize the default HTML Web service page and hide the WSDL/schema for a Web service. I hope you find this article useful. Please e-mail me with any comments or question you may have. About the Author Manuj Aggarwal has been developing applications on Microsoft platforms for more than 6 years. He has extensive experience with B2B and EAI solutions using technologies like SOAP, Web Services, EDI, HL7, etc. He has been working on integration projects using .NET since its beta. He received his MCSD certification in 1998 for VB6 and recently received the MCSD Early Achiever for .NET. He can be reached at manuj@canada.com.
|