Sunday, September 23, 2007

Writing .NET WebServices with comments

As all programmers know it's very important to write documentation of the code. But it's also importent to write comments and instructions so the users of your code understand what your methods do. In a public function you just use the default documentation style in .NET. To start documenting a function just place the cursor on the line before the start of the function and write ///. When you do that Visual Studio will generate a template for documentation of your function. The information you type here will be visible when you use the function.

To write documentation for users of your WebService you have to take a different approach. The reason is that the default comments is not visible to the user of the WebService. You will have to write a little bit of html code to get it visible.

Add the following to the top of your WebService:

[WebService(Namespace="http://www.foo.com", Description="Html description of your WebService")]
public class YourWebService : System.Web.Services.WebService {
...

For each function add the following:

[WebMethod(Description="Html description of your function")]
public string YourFunction(){
...

The descriptions will be visible to the user when he browses to your asmx-page. There are also other properties availible for the WebMethod - Take a lock at MSDN.

0 comments: