Wednesday, December 19, 2007

Creating .NET Web Services Step 1 , 2

Aside from your typical data namespace import, you also add the Web Services namespace:

using System.Web.Services

In VB this would be:
Imports System.Web.ServicesHere I've added the [WebService(Description="My Suppliers List Web Service")], which gives a custom description. Next we create our class, which, in order that it be exposed, and able to inherit the Webservice base class, must be a public class.
public class GetInfo : WebService {
Now we mark the method that’s to be exposed via the WebMethod attribute. Every class that’s enclosed within a WebMethod will be exposed as a service. Also, I boost performance by setting the BufferResponse to true.
[WebMethod (BufferResponse=true)]
If you prefer to have the description right below the exposed WebMethod link, you can achieve it like this in C#:
[WebMethod (Description="My Suppliers List Web Service",BufferResponse=true)]
The VB version would incorporate the description and BufferResponse in one statement, namely WebMethod, that used angle brackets. This is placed after the class creation and Web service inheriting line, and on the same line with, but before the function, like so:
<WebMethod(Description:="My Suppliers List Web Service",BufferResponse:=True)> Public Function ShowSuppliers (ByVal str As String) As DataSet
Next, we set up our method or VB function, which will accept a string parameter from our drop down list and pass this to our SQL query (you'll see all this in detail in Step 4). If you're really savvy you can pass this and other parameters with the help of C# structs (scaled down classes) to enumerate your data value types, and provide you with better memory allocation. But for now, we'll stick to the basics:
public DataSet ShowSuppliers (string str)
Here, we set up our dataset method to return us exactly that. Lastly, we perform typical data connection and access and return our results, and we're done! So far so good? After viewing this in your browser, the above code should to start to make sense. Let's go to Step 2.

Step 2 -
Consume the Web Service Source FileNext, append ?WSDL to the Web services URI (Uniform Resource Identifier) like so:
http://localhost/suppliers.asmx?WSDL
“What's this?” you ask. This is the WSDL document that the client will use to access this service.
Even so, you don't have to know much about this unreadable code to produce results, which is where the WSDL.exe command-line tool comes into play. Due to the open protocol nature of Web services, this tool enables you to consume non-.NET Web Services as well.
You can bypass WSDL and test the XML results instantly through HTTP GET protocol, by typing the following into your browser:
http://localhost/suppliers.asmx/ShowSuppliers?str=USA
This passes USA as a parameter to the ShowSuppliers class method. Note that if you're using .NET SDK Beta 1.1 (v.1.1.4322), it seems to prefer HTTP POST protocol, so invoke the Web Service through its exposed method link. The XML results? A little crazy, huh?
Nevertheless, scroll down a little and you'll see the query results of our class call. Later in Step 4, all this gobbledygook will make much more readable sense.
So, to create our proxy class sourcefile, make a batch file named makeWS.bat and type in:
[C#] wsdl.exe /l:CS /n:WService /out:bin/GetSuppliers.cs http://localhost/suppliers.asmx?WSDL
[VB]
wsdl.exe /l:VB /n:WService /out:bin/GetSuppliers.vb http://localhost/suppliers.asmx?WSDL pause
This tells our WSDL tool that /l is the language you’re using.
/n: creates the Wservice namespace so you can reference it from your .aspx page, and finally send the C#/VB source file to the bin folder from which we'll create the dll assembly that we’ll

use in Step 3.
Also add pause, so after this is all said and done, you'll be prompted to press any key to continue, and more importantly, you’ll know for sure that it all went according to plan.
Now locate your new batch file, and double-click on it to run it. Once you’ve done this, you will have created, rather consumed, the proxy class or source file GetSuppliers.cs right from the .asmx file. Have a look in your bin folder.
Note that you could run these commands via the command-line prompt, but for the sake of convenience, I prefer the good old' batch file. You may be wondering where the phrase “consuming” came from? In this regard, it simply refers to the presentation of the Web Service to the "consumer”.

No comments: