asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Creating a Custom Web Control To Consume an XML Web Service
By Robert Chartier
Rating: 3.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction


    After reading this article, you will see how easy it is to set up custom Web controls in the new Microsoft .NET platform to consume an XML Web service. If you are not familiar with custom Web controls, here is a partial definition from the Microsoft documentation:

    Web controls run on the server and include form controls such as buttons and text boxes, as well as special purpose controls such as a calendar. This allows you to programmatically control these elements on a Web page. Web controls are more abstract than HTML controls. Their object model does not necessarily reflect HTML syntax. (See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControls.asp.)

    We are going to create a special purpose control to simply display some text on the page.

    An XML Web service, according to the Microsoft documentation, can be defined as

    A Web Service is a programmable entity residing on a Web Server exposed via standard Internet protocols. (See http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebServices.asp.)

    This article provides you with instructions on how to use a simple text editor and the DOS prompt to create a Web server control that gets its data from an eXtensible Markup Language (XML) Web service and simply displays the results on the page. This is very useful since the actual Web server control is a single package. All you need to do is provide people with your assemblies (.DLL files), and then they can simply drag and drop your Web server control onto their pages and the control will take care of the rest. This makes for a very modular approach at site design because it encapsulates many of the redundant tasks normally encountered when developing any type of Web application.

    Getting the Data

    There are so many choices right now for good, viable XML Web services on the market. I have decided to use a Web service that will produce quotes (from famous or not so famous authors) for this article. Our first task would be to seek out and determine the best available Web service on the market right now. There are currently three major players listing Public XML Web services:

    1. SANTRA Technology's iON service -- http://www.mysantra.com
    2. XMethods' directory of Web services -- http://www.xmethods.com
    3. Sal Services Web services brokerage -- http://www.salcentral.com

      Personally, I prefer to use my own site (http://www.mysantra.com), because of its advanced features, like monitoring and alerting, uptime statistics, and the real handy Compare Services feature. I chose to use to use the Web service that Santra publishes after much searching and comparing. One important thing found on Santra's Service Details page (http://www.mysantra.com/MyService_Details.asp?service_id=358) is the location of the WSDL file. I copy (clipboard) the path to this WSDL file because I will need to use this later to create a Reference in my project.

      Setting Up

      We start by creating our project directory and importing our Web Service Reference. I've decided to place all of this project's files in the "c:\inetpub\WebControls\Quote\" directory.

      Let's bring up a DOS command window by going to Start, Run, and type in "cmd" (without the quotes) and hit Enter. Navigate to your appropriate directory (c:\inetpub\WebControls\Quote\). We are now ready to import the Web service.

      Within the .NET Framework there is a handy utility called wsdl.exe. The Web Services Description Language (WSDL) tool generates code for ASP.NET Web services and Web service clients from WSDL contract files, XSD schemas, and .discomap discovery documents. (See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfwebservicesdescriptionlanguagetoolwsdlexe.asp.) This tool can be used in conjunction with disco.exe.

      (DOS command help. Type: wsdl /?)

      Look through the help for the WSDL.EXE utility. It is fairly straightforward. (A full description is out of the scope of our current discussion.) The command we will use is fairly simple:

      wsdl /namespace:SantraQuote http://rob.santra.com/webservices/public/quote/index.asmx?wsdl

      The output is:

      Microsoft (R) Web Services Description Language Utility
      [Microsoft (R) .NET Framework, Version 1.0.3328.4]
      Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

      Writing file 'C:\Inetpub\WebControls\Quote\QuoteService.cs'.

      This will use the XML Web service given at the location of the URL we specified to create a proxy class with the namespace SantraQuote. It automatically creates the file for us given in the output above. Let's open up this generated class and add our Web control code into this class.

      Why are we adding in the Web control code directly into this generated proxy class? Because we want to have the entire control in one self-containing DLL for distribution. One could create a separate namespace and class for the Web control, which would reference this generated proxy so it could be reused from other portions of the application. Since we will be only using it for this one Web control, I've decided to encapsulate all of the code in the same file and namespace.

      Code the Web Control

      Within our QuoteService.cs file we see there is a lot of code dealing with consuming the Web service. Ignore all of this. First, we need to add in our references for the UI and WebControls namespaces.

      
          using System.Web.UI;
          using System.Web.UI.WebControls;
      
      
      And then we add our QuoteControl class that simply has one method (Render) that consumes the generated QuoteService class and writes out the results of the "Quote" method.
      
          public class QuoteControl : Control {
            QuoteService QS = new QuoteService();
            protected override void Render( HtmlTextWriter writer)  {
              writer.Write(QS.Quote());
            }
          }    
      
      
      Now, let's compile this namespace. At the DOS prompt we will use the C# compiler. Again, you should review its full capabilities (csc /?). For this demonstration we will just use the following command:

      csc /t:library /out:C:\Inetpub\wwwroot\bin\QuoteServiceControl.dll QuoteService.cs

      The output is:

      Microsoft (R) Visual C# .NET Compiler version 7.00.9372.1
      for Microsoft (R) .NET Framework version 1.0.3328
      Copyright (C) Microsoft Corporation 2001. All rights reserved.

      This means it was a successful build with no errors. If you receive any errors, there should be a detailed explanation of what and where the error is. Just do some simple debugging to bring your version up to speed.

      The /t flag is used to indicate what the target assembly will be. We want a library that will be used in other projects. Lastly, we used the /out: parameter to simply give the DLL a more useful name and place in our bin directory off of our Web root.

      Now, what did this give us? Well if you look in the bin directory off of our Web root, we will see a file "QuoteServiceControl.dll." This contains all of the code to consume the XML Web service and for us to use it as a Web server control in our ASPX pages.

      Code the ASP.NET Test Page

      For simplicity sake we will still use our text editor and not the Visual Studio.NET (VS.NET) tool. In order to quickly test our control, we will create a small test page (c:\inetpub\wwwroot\quote\index.aspx) with the two essential lines to consume a Web service control.

      The first line:

      
      <%@ Register TagPrefix="CTL" Namespace="SantraQuote" Assembly="QuoteServiceControl" %>
      
      
      Notice the <%@ Register line. This registers the assembly (file name of the DLL) with the given namespace and binds it to the given TagPrefix. This TagPrefix is an arbitrary value that is used later to place the control onto the form, like:
      
          <CTL:QuoteControl runat="server" />
      
      
      This final line of code adds our control to the page.

      The full listing is:

      
      <%@ Register TagPrefix="CTL" Namespace="SantraQuote" Assembly="QuoteServiceControl" %>
      <html>
      <body>   
          Random Quote:
          <CTL:QuoteControl runat="server" />
      </body>
      </html>
      
      
      All we need to do is to test this in our Web browser. Point your browser to http://localhost/quote. The first time it should take a few seconds to compile the page, etc., and then it should show you a random quote. Notice that if you hit "Reload," the page loads much faster.

      Next, we will move on and show you how to use this control in the VS.NET environment.

      Using the Control in VS.NET

      Let's start off by first creating a new C# ASP.NET application at the location http://localhost/Quote.NET. Once the development environment has created your blank project for you, we must first add the component to your toolbox. On the left side you will see the toolbox. Choose the "Components" tab, right click, and choose "Customize Toolbox". In the .NET Framework Components tab, use the "Browse" button to locate our DLL at c:\inetpub\wwwroot\bin\QuoteServiceControl.dll. We see that it actually finds two classes within our single namespace, and since we only need the one "QuoteControl", let's simply uncheck the "QuoteService" item and hit the "OK" button. Now, you should see a "QuoteControl" item listed in our toolbox. Click and drag this Web control onto your Webform1.aspx page. Immediately, you should see that it actually goes ahead and consumes the Web service, and shows you an example of what it will look like. If you wish, you can use the HTML pane to edit and format the result. Before we test this, rename the default Web form to "index.aspx". When you have finished editing, hit the Play button and notice that it will create a new instance of Internet Explorer for you, with our page loaded, and that it does in fact work. Close the browser and return to the normal development environment. You now have consumed our sample Web server control in the VS.NET environment. From now on, you can view that simply by visiting the URL http://localhost/Quote.NET/index.aspx. It is already live for you!

      Conclusion

      I hope that this has given you the basic understanding and ability to start creating your own Web controls. If you have any problems with this or any other article I have written, please feel free to contact me at any time.

    4. download complete source code
    5. view demo

      About the Author

      Robert Chartier has worked in the information technologies field for more than seven years. While studying at college, he began his career working as a software and hardware technician at the college, supporting a user base of thousands of students and hundreds of instructors. Once his college days were finished he moved on to full-time studies at a university in the lower mainland of British Columbia, and landed a full-time job developing large projects for distribution on many platforms, mediums, and languages.

      His next position enabled him to tap into the Internet development market on a larger and more focused scale. In his spare time he began writing and producing content for developer-specific sites focusing on Microsoft technologies (ASP, COM/COM+, etc.). He has also been a part of many open forums on cutting-edge technologies, such as the .NET Framework and Web services (SOAP), and has been invited to speak at large developer conferences and contribute to many technical publications.

      His next step was to take a position with a large B2B training marketplace, where he developed many tools, including a very comprehensive search engine with custom business rules for weighting, sorting, and analysis (COM+). This led him into strong development with beta versions of Commerce Server 2000 and BizTalk Server 2000. His next opportunity included a large Internet development effort using technologies such as JSP (Java Beans, J2EE), Oracle, WebLogic, etc. His current position as vice president of technology for Santra Technology (http://www.santra.com) has allowed him to focus more time on the Web services market space. Santra is an award-winning, industry leader in this cutting-edge technology, focusing on Web service monitoring, alerting, and performance measurement.

      Robert Chartier can be reached at rob@aspfree.com.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jul 7, 2005 - Hosting Indigo Web Services
    In the second article of his series on Indigo web services, Chris Peiris explains how to host an Indigo web service and examines the IIS, self hosting, and Windows Activation Service hosting options. He then provides step-by-step instructions and sample code for an IIS-hosted and self-hosted Indigo web service.
    [Read This Article]  [Top]
    Jun 8, 2005 - Indigo Programming Model
    In the first part of his series on Microsoft Indigo, Chris Peiris examines the basics of SOA, explains how Indigo fits into the picture and the problems it solves. He then introduces Indigo's programming model and finishes by building a sample Indigo web service using the Microsoft .Net Framework 2.0.
    [Read This Article]  [Top]
    Nov 10, 2004 - Business Intelligence with Microsoft SQL Server Reporting Services - Part 3
    Adnan Masood concludes his discussion of Microsoft SQL Server Analysis services and Microsoft SQL Server Reporting services. In the final part, he discusses Reporting Server web services and using custom code in reports.
    [Read This Article]  [Top]
    Jul 8, 2004 - Using IE's Web Service Behavior To Create Rich ASP.NET Applications
    This article explains the features of the IE Web service behavior and shows how to asynchronously communicate with an ASP.NET Web service directly from the client.
    [Read This Article]  [Top]
    Jul 6, 2004 - Using .NET and Excel 2003 To Validate E-Mails
    Calvin Luttrell shows how to validate e-mail addresses stored in Excel 2003 and provides a special function for solving that pesky problem Yahoo! mail servers cause.
    [Read This Article]  [Top]
    Jun 9, 2004 - Modifying Web Services Documentation
    This short article describes a quick and easy way to provide some security to an ASP.NET Web service by modifying its associated documentation file.
    [Read This Article]  [Top]
    Jun 2, 2004 - Kerberos Authentication with Web Services Enhancements 2.0
    Kerberos authentication is the cornerstone of Windows operating system authentication architecture. Web Services Enhancement 2.0 (WSE 2.0) extends Kerberos support to ASP.NET Web services. Chris Peiris explains the support for this new feature in WSE 2.0.
    [Read This Article]  [Top]
    Dec 15, 2003 - Realizing a Service-Oriented Architecture with .NET
    Chip Irek examines the architectural issues and component design issues of building a .NET application in a service-oriented architecture.
    [Read This Article]  [Top]
    Nov 24, 2003 - Consuming Asynchronous Web Services
    Thiru Thangarathinam shows how to use asynchronous Web services, Windows Service applications, server-based timer components and .NET XML API classes to create high-performance, scalable, and flexible applications.
    [Read This Article]  [Top]
    Nov 12, 2003 - Implementing Paging and XSLT Extensions Using XSLT in .NET - Part 2
    Part one showed how to transform XML data into HTML by using an XSL stylesheet from within a .NET application. This part explains how to make use of XSLT Extension objects and invoke a C# class method from an XSL stylesheet.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs