SYS-CON Media
 Register Now!
Save $200
Register before October 17th to SAVE! ... and also receive a FREE copy of the Best-Selling AJAX Book, a $119 Value!
Untitled Document
2008 East
Platinum Sponsor
Untitled Document
2008 East Gold Sponsors
Untitled Document
2008 East Exhibitors
Untitled Document
2008 East Media Sponsors
Untitled Document
2008 Association Sponsor
SYS-CON Media
2007 West Sponsors
Goingtomeet.com Conference Directory
SYS-CON Media
2007 East Sponsors
Untitled Document
2008 SYS-CON Events

Can't Miss RSS Feed
Subscribe to the AJAXWorld.com RSS Feed & Get All The Conference News As It Happens!

2008: Decision Year for RIAs - October 20-22, 2008 San Jose


Building Web Services Using Microsoft .NET
Building Web Services Using Microsoft .NET

Microsoft .NET is the key enabling technology for Microsoft's vision of software as a service. The .NET Framework is the overall infrastructure that provides developers with a platform to create programs that transcend device boundaries and fully harness the connectivity of the Internet.

In this article, I'll illustrate how easy it is to build and use Web services using the .NET Framework SDK. I used the Beta 2 of the .NET Framework SDK and wrote all code samples using C#, Microsoft's new programming language for .NET. This article assumes you have some familiarity with Web services concepts and component-based programming. While I've tried my best to ensure that the technical content of this article is up-to-date, some features and operations in the .NET Framework might change when a newer version of the SDK ships. To try out the source code in this article, you need to download and install the Microsoft .NET Framework SDK Beta 2 from www.microsoft.com/net/downloads.asp. The SDK includes everything developers need to write, build, test, and deploy .NET Framework applications - documentation, tools, compilers, and samples. You don't need to use Visual Studio .NET to run the code samples in this article. I installed the SDK on my Windows 2000 server machine and used Notepad as my source code editor.

.NET Framework Overview
The .NET Framework is a platform for building, deploying, and running Web services and applications. It provides a highly productive, standards-based, multi-language development environment and runtime infrastructure that simplifies application development in the Windows platform. The .NET Framework was built from the ground up to meet the needs of Web services developers and consumers. It has pervasive support for XML and SOAP. The general design goals of the NET Framework are to:

  • Simplify application development
  • Unify programming models across all languages and types of applications
  • Utilize Web standards for better interoperability with other platforms
  • Make it easier to deploy, version, and maintain applications

    As you can see in Figure 1, the .NET Framework is essentially a system application running on top of the operating system, which can be different flavors of Windows and even other operating systems.

    The most important component of the .NET Framework is the Common Language Runtime (CLR). The CLR is a rich runtime environment that provides important system services to .NET objects written in any language that can be represented in the Common Intermediate Language (CIL). The CLR activates objects, performs code access security, lays out objects in memory, verifies type safety, performs garbage collection, and facilitates error handling. Java developers may think of the CLR as the .NET equivalent of the Java Virtual Machine (VM).

    Sitting on top of the CLR is the Base Class Library, consisting of classes that support IO functions, string manip-ulation, security management, threading, reflection function-ality, collections, and other functions.

    On top of the base class library is a set of classes that provide data and XML manipulation. ADO. NET and SQL classes allow you to access and manipulate persistent data stored on backend databases. The framework also provides a number of classes to manipulate XML, perform XML searches, and perform XML transformations.

    Win Forms (or Windows Forms) provides classes for development of native Windows GUI applications. If you've developed MFC applications in the past, you'll fall in love with Win Forms since it supports easier GUI development and provides a common and consistent interface across all languages. ASP.NET is the Web application platform that enables developers to easily develop both powerful Web services and use Web Forms to build rich browser-based applications.

    System.Web.Services Namespace
    The System.Web.Services namespace in the .NET Framework SDK consists of classes for developing Web services:

  • WebService: Optional base class for classes implementing the Web service. This provides direct access to common ASP.NET objects such as Application and Session.
  • WebServiceAttribute: Attribute used to provide additional information to a Web service such as a description string and a custom XML namespace.
  • WebMethodAttribute: Attribute used to expose a public method of the implementing class to remote Web clients.
  • WebServiceBindingAttribute: Attribute used to declare the binding of one or more Web service methods.

    Web Services in ASP.NET
    ASP.NET provides a development platform that makes it easy for developers to build, publish, and consume Web services. It provides the infrastructure for the inner workings of Web services so developers can focus on implementing the business functionality instead of worrying about the low-level infrastructure details. Within ASP.NET, there are two important pieces of Web services technology: ASP.NET Web services and ASP.NET Web Service Clients.

    ASP.NETWeb Services is a technology that allows you to build and expose Web services. Since ASP.NET Web Services are built on top of ASP.NET, you can take advantage of the features of ASP.NET such as caching, state management, and authentication, which are all built into the framework. Also, all ASP.NET codes are compiled rather than interpreted, which offers significant performance improve-ments over the current Active Server Pages (ASP) programming model.

    ASP.NET Web Service Clients pertains to any components or applications that reference and make use of a Web service. This reference is made possible through a proxy class for the Web service.

    In the following sections, I'll show you how to build and consume a Web service using ASP.NET.

    Building a .NET Web Service
    Creating a Web service in ASP.NET is similar to creating any component that exposes programmatic access to its application logic. You typically create a Web service in two steps: Declare the Web service and define the Web service methods.

    Declare the Web service
    In ASP.NET, you declare a Web service by placing the required  . < % @WebService ... %> directive at the top of a text file with a .asmx filename extension. In this directive, you must specify values for both the Language and Class attributes. The Language attribute tells ASP.NET which compiler to use for the Web service. You can set this attribute to C#, VB, and JS, which refer to C#, Visual Basic.NET, and JScript.NET, respectively. The Class attribute tells ASP.NET which class is implementing the Web service. The implementing class can reside either on the same file or in an assembly. The code example in Listing 1 shows how to set the Class attribute to a class residing in the same file.

    The class implementing the Web service doesn't need to reside on the same .asmx file. You can achieve a higher degree of code reusability by placing the implementing class in an assembly. Your .asmx file will contain only the WebService directive, and the assembly needs to reside under the \bin directory of the Web application hosting the Web service. This is illustrated in Listing 2. MyAssembly.cs is compiled with the C# compiler (csc.exe) to MyAssembly.dll using the following command:

    csc /t:library MyAssembly.cs

    In the WebService directive in Listing 2, the implementing class name and the assembly name are both specified. If the assembly name is not specified, ASP.NET searches through the list of assemblies in the \bin directory of the Web application the first time the Web service is accessed. Therefore, specifying the assembly name will yield better performance on the first access of the Web service.

    Classes implementing Web services can optionally derive from the WebService class of the System.Web.Services namespace. This will allow your implementing class to have access to all the common ASP.NET objects such as Application and Session objects. These common ASP.NET objects can be accessed via the Context property of the WebService class.

    Your Web service, by default, will use the XML namespace http://tempuri.org/. I would highly recommend that you specify your own namespace for your Web service before it is made available publicly. To do this, apply the optional WebService attribute to the class implementing your Web service and specify your own namespace in the Namespace attribute. This will distinguish your Web service from other Web services that might be using the default namespace. The code example in Listing 3 illustrates overriding the default XML namespace by specifying a custom XML namespace.

    Define the Web Service Methods
    By default, public methods of a class implementing your Web service cannot be invoked over the web. For every method you want to expose to the Web, you need to apply a WebMethod attribute. These methods are called Web Service Methods. You don't need to tag every method in your class as WebMethod unless you want that method to be accessible via the Web. Listing 4 shows a Web Service Method (Hello) that's accessible to Web service clients and a regular method (DoSomeWork) that won't be accessible from the Web.

    I created a sample Web service that provides basic mathematical functions such as Addition, Subtraction, Multiplication, and Division (see Listing 5). The Web service resides in a file called Calculator.asmx. To set up the Web service on my local box, I used IIS 5.0 Internet Services Manager to create a virtual directory called WebServices. I placed the Calculator.asmx file on that directory. The complete URL to this Calculator Web service is http://localhost/ WebServices/Calculator.asmx. If you deploy this sample Web service on a remote server, you'll need to substitute the name of the server and the virtual directory appropriately.

    Consuming a .NET Web Service
    Clients communicate with Web services via standard Web protocols. Microsoft .NET Web services currently support three protocols: HTTP GET, HTTP POST, and SOAP over HTTP. This means that any application running on any platform should be able to access .NET Web services as long as they are using these standard Web protocols and can understand XML-encoded messages.

    Default Web Service Consumer
    If you point your Web browser to the .asmx file of your Web service, ASP.NET will give you a list of supported methods (see Figure 2). Clicking on one of these methods will bring up a form representing a default Web service consumer (see Figure 3). This default consumer uses the HTTP GET protocol to talk with the Web service and is autogenerated on the fly via .NET reflection. This is a great way to immediately test your Web service methods without writing any client code.

    If we try invoking the Add method with 1 and 2 as input parameters, a new browser window is displayed containing the XML-encoded result shown in Figure 4. In this example, the complete URL used to invoke the Add Web method, along with parameters, is the following: http://localhost/WebServices/Calculator.asmx/Add?lNum1=1&lNum2=2.

    The default consumer also provides the description of how to access the method via SOAP, HTTP GET and HTTP POST. These descriptions help developers understand the encoding of the request and response messages, which is often critical when interoperating with a non-.NET Web service consumer.

    Web Services Description Language Tool
    The Microsoft .NET Framework SDK provides a utility, Web Services Description Language Tool (Wsdl.exe), to generate source code proxies to the actual Web services. This utility uses the WSDL document of the Web service to generate these proxies. The .NET Framework uses reflection to dynamically generate the WSDL document for the Calculator Web service via the URL http://localhost/WebServices/Calculator. asmx?WSDL. The Wsdl.exe tool can also take a WSDL file as its input, instead of a URL pointing to where the WSDL can be obtained. This will apply to scenarios where you'll be generating client proxies against non-.NET Web services.

    When you create a proxy class using Wsdl.exe, the default protocol is SOAP. SOAP provides the richest extensibility mechanism of the three protocols supported by ASP.NET in the current release by allowing scenarios where classes and structs can be passed as arguments to the methods of the Web service. If you want to generate client proxy code for the Calculator Web service in the C# language and use the SOAP protocol, you'll need to execute the following in the command line:

    wsdl /l:CS /o:CalculatorProxy.cs
    /protocol:SOAP
    http://localhost/WebServices/Calculator.asmx?WSDL

    This command will generate a C# source file called CalculatorProxy.cs containing a proxy class, Calculator, that derives from the SoapHttpClientProtocol class.

    The /protocol: parameter specifies which protocol you want to use. Since SOAP is the default protocol used by Wsdl.exe, you can omit the /protocol: parameter from the above call to Wsdl.exe. If you want to use the HTTP GET protocol, specify HttpGet. The proxy class generated will derive from the HttpGetClient Protocol class. If you want to use the HTTP POST protocol, you need to specify HttpPost and the proxy class generated will derive from the HttpPostClientProtocol class.

    Inspecting the generated proxy class, it includes methods that forward calls to the original Web service methods. You have two choices for how this proxy can be used in your client application. The first option is to include this source file in the client application project. However, the client application project needs to be a C# project. You also need to add into your project any references that the proxy depends on.

    A better option for using the proxy class is to compile the C# proxy source file into a dynamic link library (DLL) and then add a reference to this DLL to any client project you want to create. This way, your proxy DLL is self-contained and can be used by different projects implemented in different .NET languages. Below is the command line for compiling the C# proxy source into a DLL (Calculator Proxy.dll):

    csc /t:library /r:system.web.services.dll
    /r:system.xml.dll CalculatorProxy.cs

    Now let's look at the client application code that will use this proxy DLL (see Listing 6). You compile the client code to a console program (CalculatorClient.exe) using the following command line:

    csc CalculatorClient.cs /r:CalculatorProxy.dll

    No matter how you choose to use the proxy class, the client code will still look the same. First, an instance of the proxy class is created. Then, the appropriate methods of the proxy class are called that will in turn invoke the Web service methods. In this case, the Multiply method was invoked, passing in the value of 4 for both parameters. The resulting value was displayed on the console screen. You should see the following if you run CalculatorClient .exe from the command line:

    4 X 4 = 16

    That's all there is to it. Both the proxy class and the .NET Framework do all the dirty work of creating and parsing SOAP messages between the Web service and the client application.

    Summary
    This article took you on a brief tour of using the Microsoft .NET Framework for building and consuming Web services. As you can see, ASP.NET makes it very easy to write and consume Web services without having to dive into the details of standard specifications such as HTTP, SOAP and WSDL. Web service is a strategic technology in Microsoft's overall vision. This is very evident in the framework's extensive support for Web services and Microsoft's recent announcement of Project Hailstorm - a set of user-centric Web services that includes identification and authentication, e-mail, instant messaging, automated alerts, calendar, address book, and online file storage. I strongly encourage everyone developing in the Microsoft platform to start playing with Microsoft .NET Framework SDK and Visual Studio .NET. Use the information in this article to start writing your own Web services and create programs that consume existing Web services.

    For more information on .NET Web services, go to http://msdn.microsoft.com/webservices/. For more general information on Microsoft .NET, visit www.microsoft.com/net/.

    About Jonathan Cortez
    Jonathan Cortez is a software architect and technical lead with Cap Gemini Ernst & Young. He develops world-class e-commerce and knowledge management solutions at the company's Advanced Development Center, based in Bellevue, WA. He specializes in .NET and COM+ development using C#, C++/ATL, VB, SQL Server, XML, and other Web technologies. He just completed a project where he led a team in the development of Web services using SOAP for a large online stock brokerage

  • In order to post a comment you need to be registered and logged in.

    Register | Sign-in

    Reader Feedback: Page 1 of 1

    Latest AJAXWorld RIA Stories
    "We're developing a software that is based on the cloud environment and we are providing those services to corporations and the general public," explained Seungmin Kim, CEO/CTO of SM Systems Inc., in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the ...
    "MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – N...
    In his session at 21st Cloud Expo, James Henry, Co-CEO/CTO of Calgary Scientific Inc., introduced you to the challenges, solutions and benefits of training AI systems to solve visual problems with an emphasis on improving AIs with continuous training in the field. He explored app...
    The question before companies today is not whether to become intelligent, it’s a question of how and how fast. The key is to adopt and deploy an intelligent application strategy while simultaneously preparing to scale that intelligence. In her session at 21st Cloud Expo, Sangeeta...
    "We work around really protecting the confidentiality of information, and by doing so we've developed implementations of encryption through a patented process that is known as superencipherment," explained Richard Blech, CEO of Secure Channels Inc., in this SYS-CON.tv interview a...
    Untitled Document

    Call 201 802-3020 or Click Here to Save $200!

    Register Today and
    Save $200

    Your registrations includes: Golden Pass Delegates will receive full conference access on October 20-22, 2008 including: Lunch and Coffee Breaks, Collectible Bag and Archives of all sessions on DVD. Includes access to all Conference Sessions including the Technical Sessions, Exhibits, Keynotes, Vendor Technology Presentations, and Power Panels.


    Sponsorship Opportunities

    AJAXWorld offers the undisputed best platform to position your company as a leading vendor in the fast-emerging marketplace for AJAX and Enterprise Web 2.0.


    Please call
    (201)802-3020


    Who Should Attend?

     CTOs & VPs of Engineering
     Directors of Technology
     Sr. User Interface Architects
     Front-End Engineers
     VCs & Industry Analysts
     Directors of Business Development
     Software Engineers
     Senior Architects
     Application Programmers & Software Developers
     Project Managers
     Web Programmers & Designers
     Companies & Organizations that need to stay in
      front of the latest Web technology

    AJAXWorld Security Bootcamp

    Introducing at AJAXWorld RIA Conference 2008 West the world's first-ever full, one-day immersive "AJAX Security Bootcamp" - led by one of the world's foremost AJAX security experts and teachers, HP's Billy Hoffman.

    View the full one-day schedule



    AJAXWorld 2008 West - Tracks

    Track 01: Enterprise RIAs
    Track 02: Frameworks & Toolkits
    Track 03: Web 2.0 & Mashups
    Track 04: Hot Topics
    Track 05: The Future of the Web
    Track 06: iPhone Developer Summit



    Brought To You By:

    AJAXWorld Magazine is the pre-eminent independent vendor-neutral resource for the fastest growing new segment of the software business: entirely Web-based applications and experiences.

    Download the Latest Issue!

    AJAXWorld Webcasts



    SYS-CON EVENTS


    AJAXWorld Keynotes & Power Panels

    2008 SYS-CON TV Keynotes: Can We Fix the Web? By Douglas Crockford - by Douglas Crockford
    2008 SYS-CON TV Keynotes: 2008: The Year of the RIA - by Anthony Franco
    2008 SYS-CON TV Power Panel: The Business Value of RIAs
    2008 SYS-CON TV Power Panel: What Lies Beyond AJAX
    2007 SYS-CON TV Keynotes: Why Web 2.0 for the Enterprise Is Far More Than Just a Facelift - by Ted Farrell
    2007 SYS-CON TV Keynotes: Fueling the Next Generation Web: A Peek Behind the Green Curtain - by Bob Brewin
    2007 SYS-CON TV Keynotes: AJAX in the Balance - by Joe Stagner

    AJAXWorld Sessions on SYS-CON.TV

    · Bill Scott - Yahoo! UI Library
    · David Heinemeier Hansson - AJAX on Rails
    · Jesse James Garrett - Elements of User Experience
    · Dion Hinchcliffe - Real World AJAX
    · Eric Miraglia - Open Source AJAX Development
    · Paul Rademacher - Mashing Up Your Web Application
    · Adam Sah - Google Gadgets
    · Doug Crockford - An Introduction to JavaScript
    · David Linthicum - Enterprise Web 2.0
    · Patrick Grady - The Imagination & Experience Web

    AJAXWorld...All The AJAX Rock Stars in One Spot!


    Past Events Archive

    SOAWorld Conference & Expo 2008 East
    soa2008east.sys-con.com
    Virtualization Conference & Expo 2008 East
    virt2008east.sys-con.com
    AJAXWorld 2008 Conference & Expo East
    ajaxmar08.sys-con.com
    SOAWorld Conference & Expo 2007 West
    www.soaworld2007.com
    Virtualization Conference & Expo 2007 West
    virt2007west.sys-con.com
    AJAXWorld 2007 Conference & Expo West
    ajaxoct07.sys-con.com
    SOAWorld Conference & Expo 2007 East
    soa2007east.sys-con.com
    Virtualization Conference & Expo 2007 East
    virt2007east.sys-con.com
    AJAXWorld 2007 Conference & Expo East
    ajaxmarch07.sys-con.com
    Other SYS-CON Events
    events.sys-con.com

    Join Over 10,000 Early AJAX Adopters
    Who Have Attended AJAXWorld
    • A&R Edelman
    • Academic Enterprise
    • Accoona Corp [2 delegates]
    • Acxiom
    • Adams Capital Management
    • Adaptive Edge
    • Adaptive Path
    • Adobe Systems Incorporated [21 delegates]
    • Adobe Systems Romania
    • Ajax13
    • All Risks, Ltd.
    • alliance
    • Alliance For Community Care
    • AlphaDetail Inc
    • Altera Corporation
    • Amazon.com [6 delegates]
    • Appeon Corporation [2 delegates]
    • Apple Computer [5 delegates]
    • Apress [3 delegates]
    • Arkivio
    • ASA
    • Astute Solutions
    • Avaya Inc [2 delegates]
    • Avenda Systems
    • Avenue A | Razorfish [3 delegates]
    • Axcella, LLC [2 delegates]
    • Aximsoft
    • Azimyth
    • Backbase USA Inc. [4 delegates]
    • BAE Systems [2 delegates]
    • Bank of America [2 delegates]
    • Barkley Evergreen & Partners Interactive
    • Bayview Financial [2 delegates]
    • BEA Systems [3 delegates]
    • Billeo
    • BMC Software, Inc. [2 delegates]
    • Borland Software Corporation
    • Bradford Technologies, Inc [2 delegates]
    • Brilliance
    • Brocade Communications Systems, Inc. [2 delegates]
    • Brookside Capital LLC
    • Brulant
    • Bungee Labs, Inc [6 delegates]
    • Bureau of Labor Statistics
    • BUZ Interactive
    • Cadena Software
    • Calix Networks
    • Callidus Software [2 delegates]
    • Cambia Security
    • Carnegie Mellon West
    • Cautella, Inc.
    • CBSA
    • Celequest [3 delegates]
    • Change Vision, Inc.
    • Charles E. Kenney, CPA
    • Charles Schwab & Co., Inc. [8 delegates]

       read more...


    ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

    .NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
    Open Web Developer's Journal - OPEN WEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
    PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
    WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
    Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2

    SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
    SYS-CON EVENTS  |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  OpenWeb Developer Summit  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
    INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
     Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
    Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
    Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.