Build A C# Webservice Consumer WebForm Application
with Visual Studio .NET

By Peter A. Bromberg, Ph.D.
Printer-Friendly Version

Peter Bromberg  

In my previous article, "BUILD A Visual Studio .NET C# MS Search Webservice With VB 6.0 COM Interop", I detailed the "nuts and bolts" of building a webservice both with native .NET classes and, as an alternative, with COM Interop. What we will do in this article is use some of the built-in advantages of the Visual Studio .NET IDE to build a WebForm consumer of that same webservice.



What I believe is probably one of the biggest innovations of the Visual Studio .NET IDE is the ability to set a reference (webreference) to components that expose WSDL when those components reside somewhere on the Internet. Once your project is visible in the IDE, by simply right - clicking the References node in Solution Explorer, you will be presented with a context menu that includes an "Add Web Reference" option:

When you select this item, you will be presented with a large wizard dialog that asks you for the URL of the webservice WSDL (Web Service Description Language) that you want to add to your project. In the case of this particular webservice that URL is https://www.nullskull.com/webservices/msinfoservice/msinfoservice.asmx?wsdl. Your project will import the WSDL and create a fully Intellisense-enabled component reference in your project. If you build your webservices with Visual Studio .NET all of the plumbing needed to expose these methods and properties is done for you automatically. Once your reference is correctly set, it will appear in Solution explorer just as if it was a native component on your development machine:

What this means, very simply, is that suddenly the world has become your development environment! Since it is estimated that less than 2% of the world's webservices have already been built, I would venture to say that there is a real opportunity out there for the aware developer who understands the marketing maxim "Find a need and fill it".

In this consumer WebForm Application, I've added the following ASP.NET server controls:

 

lbNumDays - Listbox control to hold the choices for the number of days MS Search items to search. Valid values are 3,7,15, and 30 days. I set the 30 day item to be the default selected item.

tbSearchTerm -Textbox to hold the word or phrase the user wants to send to the MS Search Service site.

Button1 - Button control used to submit the webservice SOAP call with the values from the other controls.

Xml1 - Xml control to receive the webservice XML result, apply an XSLT transform, and display the results in a nicely - formatted HTML table.

When we go into our codebehind WebForm1.aspx.cs C# page, we can see the simple actions that represent the "meat" of the entire operation:

private void Button1_Click(object sender, System.EventArgs e)
{
string sResult="";
string sDays="";

sDays=lbNumDays.SelectedItem.Value;
string sSearch =tbSearchTerm.Text;
com.eggheadcafe.www.MSInfoService msinfo = new com.eggheadcafe.www.MSInfoService();
sResult =msinfo.GetItems(sSearch, sDays);
Xml1.DocumentContent=sResult;
Xml1.TransformSource="MSInfoResult.xsl";
}

All we do here is on the Button1_Click event handler, we grab the values from the input controls, set a new instance of our MSInfoService, and make the proper function call, setting the value of our string sResult to the return XML from the SOAP call. As you can see just below, we have full Intellisense in our IDE:

Finally, we assign this XML string to the DocumentContent property of our Xml server control, and set it's TransformSource property to the URL of our XSL stylesheet. The transform is done automatically. You can see from the snippet below that I've set the xml control inside a positioned Div so the table shows up in the correct place on the page:

<div id=disp style=" Z-INDEX: 100; LEFT: 100px; POSITION: absolute; TOP: 125px"
><asp:Xml id=Xml1 runat="server" Visible="True"></asp:Xml></div>

As if all this weren't easy enough, now comes the really pleasant surprise: I deployed this application by simply FTP-ing the folder and its contents to our webserver, and then set the folder as an application in IIS. That's it! It worked perfectly, right out of the box! Not only that, I can revise this application and send it up again while visitors are using it, and nobody will ever know the difference. I don't have to restart IIS, I don't have to register and unregister COM components, I really don't have to do ANYTHING!

If you'd like to try out the working app for size, CLICK HERE. The entire project including the stylesheet is in the zip file download at the bottom of this page. So what we've done here is created a C# WebForm Application using the codebehind technique, demonstrated how to set a reference to and consume a webservice anywhere on the Internet, created a simple web page form that accepts the inputs required from the client, then receives and transforms the XML SOAP response document and presents it as an HTML table in the page.

Man, this ain't your Father's XML! Enjoy.

Download the code that accompanies this article



Peter Bromberg is an independent consultant specializing in distributed .NET solutionsa Senior Programmer /Analyst at in Orlando and a co-developer of the NullSkull.com developer website. He can be reached at info@eggheadcafe.com