ASP.NET and Web Services Part 2

Today, Dwight takes us on a tour of the web services made available to us via ASP.NET. We learn about implementing interfaces, dynamic binding, class inheritance and interface inheritance. This piece comes from chapter seven of .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223-054-1, 2004).

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 17
March 10, 2004
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


ASP.NETWeb Services Description Language
WSDL documents contain metadata about a client’s input and output parameters from an invocation externally. This means a user is invoking a method externally. In addition, the WSDL file describes the service’s functionality. In essence, the file facilitates interaction between disparate clients by providing the requisite information on how to achieve interoperability between them.

Let’s create a small web service in ASP.NET and then inspect the WSDL file. The first task required for building a web service in the .NET world is creating a virtual directory in Internet Information Services.


  1. Create a folder on the server, and name it “MySOAPExamples.”
  2. In Windows 2000 or Windows XP, select Start | Control Panel | Administrative Tools | Internet Information Services. Open IIS, right-click the default web site, select New, and then select Virtual Directory. A dialog box will appear on your screen. Select Next, and enter MySOAPExamples in the Alias dialog box. Make sure the subdirectory is already created somewhere on the hard drive.
    Finally, proceed to Finish to generate the virtual directory.
  3. Create a new project in Visual Studio .NET called “IFCEBrokerage.”
  4. Rename Webservice1.asmx to IFCEBrokerageFirm.asmx. The extension .asmx represents an ASP.NET web service extension.
  5. The namespace should be named “namespace IFCEBrokerage.”
  6. Enter the code shown in Listing 6.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

Web Service Code Sample


Listing 6 IFCEBrokerageFirm web service code


using System
using System
.Web
using System
.Web.Services

namespace IFCEBrokerage 

  
public class SecuritiesExchange 
System.Web.Services.WebService 
  

[WebMethod]
public 
double StockQuote
(string symbol)
    
{
      double 
quotePrice
=0;
      
switch(symbol
      
{
        
case "JANS"

        quotePrice 
29.95

        
break; 
        
case 
"MSFT":
        quotePrice 

49.97;
        
break;
        
case 
"ORCL":
        quotePrice 

37.72;
        
break;
     
}
    
return 
quotePrice;
   
}
  
}
}


Several IFCEBrokerageFirm items require clarification. An excellent place to begin learning about them is in web service namespaces. Table 1 displays them with comments.

These namespaces enable developers to create XML web services using ASP.NET and XML web service clients. XML web services represent applications that enhance the ability to exchange messages in a loosely coupled environment using protocols such as HTTP, XML, XSD, SOAP, andWSDL. They facilitate constructing modular
applications in a heterogeneous environment that promotes interoperability between applications, smart client devices, and implementations.

NOTE

The WebMethodAttribute class must be applied to any method where a developer wants to expose it programmatically.

ClassDescription
WebMethodAttributeAdding this attribute to a method when defining a web service created in ASP.NET makes the method callable
from remote web clients.
Web ServiceThis class defines the optional class for XML web srvices, thereby providing access to common ASP.NET objects, for example, applications and sessions state.
WebServiceAttributeThis class is used to append additional information to an XML web service, such as a string explaining its functionality.
WebServiceBindingAttributeDeclares binding for one or more XML web service methods implemented within the class that is implementing the web service.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

The .asmx File

Because the implementation of a web service is encapsulated within a class, it is important to define an end point for the web service. The .asmx file serves as the end point for the web service. When a client calls the .asmx file, the ASP.NET runtime processes the file.

Usually, each .asmx page has a language directive at the top of the page, as displayed here:


<%@ WebService Language="C#" Class= 
IFCEBrokerage.SecuritiesExchange" %>

The first time the web service is called, the ASP.NET runtime uses the Language directive to compile the code first to MSIL and subsequently to the language specified by the directive. In this case, the directive specifies the C# .NET programming language. This means the IFCEBrokerage class code shares the CLR runtime services
and data types defined in the CTS with all other .NET programming languages.

NOTE
The Class attribute bears the fully qualified name of the class that implements the web service. Because the default language in the machine.config file is set to “vb,” the IFCEBrokerageFirm.asmx language should be set to C# .NET. Here is the machine.config file segment:


<compilation debug="false" 
explicit="true" defaultLanguage="vb">

It is also wise to place the web service’s implementation in its own assembly. By convention, the assembly is placed within the web application’s bin directory because this directory is placed within the search path. The SecuritiesExchange web service client can access this web service only through HTTP for the simple reason that ASP.NET supports only this transmission protocol. However, the web service supports three kinds of protocol bindings:

  • SOAP
  • HTTP Get
  • HTTP POST
All ASP.NET web services support SOAP because the SOAP body content is strongly typed and is XML based through the XSD Schema.
Web Services Documentation ASP.NET’s runtime includes services such as documentation. This is a major benefit because the CLR uses System.Reflection to generate two kinds of documentation:
  • Documentation utilized by clients so they can interact with web services
  • Documents referenced by people
HTML-based documents are accessible by entering the URL in a browser. Both the WebMethod and its companion attributeWebService expose the Description property.

Listing 7 demonstrates how to include documentation in the IFCEBrokerage.asmx file.

Listing 7
Adding the Description attribute


using System;
using 
System
.Web;
using System
.Web.Services;
namespace 
IFCEBrokerage
{
  
[WebService(Description ="This demonstrates how to 
use the Description attribute"
)]
  
public class SecuritiesExchange 
System.Web.Services.WebService
  
{
    
[WebMethod]
    
public double StockQuote(string 
symbol
)
    
{
      double 
quotePrice
0;
      
switch(symbol)
      
{
        
case 
"JANS":
        quotePrice 

29.95;
        
break;
        
case 
"MSFT":
        quotePrice 

49.97;
        
break;
        
case 
"ORCL":
        quotePrice 

37.72;
        
break;
      
}
      
return quotePrice;
    
}
  
}
}



Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

Automatically Generated Documentation

The documentation automatically generated for the IFCEBrokerageFirm is shown in Listing 8:

Listing 8
Sample documentation for IFCEBrokerageFirm


SecuritiesExchange
This 
demonstrates how to use the Description attribute
The following operations 
are supported
.
For a formal definition
please review the Service 
Description
.
StockQuote
This web service is using http
://tempuri.org/ as 
its default namespace.
Recommendation
Change the default namespace
before 
the XML Web service is made 
public.


Examining Namespaces

Each XML web service needs a unique namespace in order for client applications to distinguish it from other services on the Web. The http://tempuri.org/ namespace is available for XML web services that are under development, but published XML web services should use a more permanent namespace.

Your XML web service should be identified by a namespace that you control. For example, you can use your company’s Internet domain name as part of the namespace. Although many XML web service namespaces look like URLs, they need not point to actual resources on the Web. (XML web service namespaces are URIs.)

For XML web services created using ASP.NET, the default namespace can be changed using theWebService attribute’s Namespace property. TheWebService attribute is an attribute applied to the class that contains the XML web service methods. Listing 9 shows an example that sets the namespace to http://microsoft.com/webservices/.

Listing 9 HTML-based documentation


C#
[WebService(Namespace="http://microsoft.com/webservices/")]
public 
class MyWebService {
// implementation
}
Visual 
Basic.NET
<WebService(Namespace:="http://microsoft.com/
webservices/"
)> 
Public Class 
MyWebService
' implementation
End Class
For more details 
on XML namespaces,
see the W3C recommendation on Namespaces in XML.
For 
more details on WSDL, see the WSDL Specification.
For more details on URIs, 
see RFC 2396.



This web page shows the method exposed by the web service StockQuote. It also provides the following recommendation:


RecommendationChange the default namespace
before the XML web 
service is made 
public.



And it lists some examples for changing the namespace.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

The Web Service WSDL file

Listing 10 displays the web service WSDL file that strong-types the web service.

Listing 10 IFCEBrokerageFirm WSDL file


<?xml version="1.0" 
encoding="utf-8" ? >
<definitions 
xmlns
:http="http://
schemas.xmlsoap.org/wsdl/http/" 
xmlns:soap="http://
schemas.xmlsoap.org/wsdl/soap/" 
xmlns:s=
"http://www.w3.org/2001/XMLSchema" 
xmlns:s0="http://tempuri.org/"
xmlns
:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns
:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns
:mime=http://schemas.xmlsoap.org/wsdl/mime/
targetNamespace="http://tempuri.org/" 
xmlns=
"http://schemas.xmlsoap.org/wsdl/">
<types>
<s:schema 
elementFormDefault
="qualified"
targetNamespace
="http://tempuri.org/">
<s:element 
name
="StockQuote">
<s:complexType>
<s:sequence>
  
<s:element minOccurs="0" maxOccurs="1" name="symbol" type="s:string" 
/>
  
</s:sequence>
  
</s:complexType>
  
</s:element>
<s:element 
name
="StockQuoteResponse">
<s:complexType>
<s:sequence>
  
<s:element minOccurs="1" maxOccurs="1"
name
="StockQuoteResult" 
type="s:double" />
  
</s:sequence>
  
</s:complexType>
  
</s:element>
  
</s:schema>
  
</types>
<message 
name
="StockQuoteSoapIn">
  
<part name="parameters" 
element="s0:StockQuote" />
  
</message>
<message 
name
="StockQuoteSoapOut">
  
<part name="parameters" 
element="s0:StockQuoteResponse" />
  
</message>
<portType 
name
="SecuritiesExchangeSoap">
<operation 
name
="StockQuote">
  
<input message="s0:StockQuoteSoapIn" 
/>
  
<output message="s0:StockQuoteSoapOut" />
  
</operation>
  
</portType>
<binding 
name
="SecuritiesExchangeSoap" type="s0:SecuritiesExchangeSoap">
  
<soap:binding transport="http://
schemas.xmlsoap.org/soap/http" 
style="document" />
<operation name="StockQuote">
  
<soap:operation soapAction="http://
tempuri.org/StockQuote" 
style="document" />
<input>
  
<soap:body use="literal" 
/>
  
</input>
<output>
  
<soap:body 
use="literal" />
  
</output>
  
</operation>
  
</binding>
<service 
name
="SecuritiesExchange">
  
<documentation>This demonstrates 
how to 
use the Description
attribute
</documentation>
<port 
name
="SecuritiesExchangeSoap" binding="s0:SecuritiesExchangeSoap">
  
<soap:address 
location
="http://
localhost/IFCEBrokerage/IFCEBrokerageFirm.asmx" 
/>
  
</port>
  
</service>
  
</definitions>

TheWSDL file has several interesting characteristics, described shortly in some detail.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

The Standard WSDL File Form

But first, Listing 11 demonstrates the standard form that every WSDL file takes.

Listing 11 IFCEBrokerageFirm.asmx WSDL file


<?xml version="1.0" encoding="utf-8" 
>

<definitions 
xmlns
:http="http://
schemas.xmlsoap.org/wsdl/http//

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns
:s="http://www.w3.org/2001/XMLSchema

xmlns:s0="http://tempuri.org/" 
xmlns:soapenc=
"http://schemas.xmlsoap.org/
soap/encoding/" 
xmlns:tm="http://microsoft.com/wsdl/
mime/textMatching/" 
xmlns:mime="http://
schemas.xmlsoap.org/wsdl/mime/"

  
targetNamespace
="http://tempuri.org/"
xmlns
="http://schemas.xmlsoap.org/wsdl/">
<types>
<s:schema 
elementFormDefault
="qualified"
targetNamespace
="http://tempuri.org/">
<s:element 
name
="StockQuote">
  
<s:element minOccurs="0" maxOccurs="1" 
name="symbol" type="s:string" />
<s:element 
name
="StockQuoteResponse">
<s:complexType> <s:element 
minOccurs
="1"
 maxOccurs
="1" name="StockQuoteResult"
type
="s:double" 
/>
  
</s:sequence> </types>
<message 
name
="StockQuoteSoapIn">
  
<part name="parameters" 
element="s0:StockQuote" />
  
</message>
<message 
name
="StockQuoteSoapOut">
  
<part name="parameters" 
element="s0:StockQuoteResponse" />
  
</message>
<portType 
name
="SecuritiesExchangeSoap">
<operation 
name
="StockQuote">
  
<input message="s0:StockQuoteSoapIn" 
/>
  
<output message="s0:StockQuoteSoapOut" />
  
</operation>
  
</portType>
<binding 
name
="SecuritiesExchangeSoap" type="s0:SecuritiesExchangeSoap">
  
<soap:binding transport="http://
schemas.xmlsoap.org/soap/http" 
style="document" />
<operation name="StockQuote">
  
<soap:operation soapAction="http://
tempuri.org/StockQuote" 
style="document" />
<input> <soap:body use="literal" 
/>
</input>
<output> <soap:body use="literal" 
/>
<service name="SecuritiesExchange">
<port 
name
="SecuritiesExchangeSoap" binding="s0:SecuritiesExchangeSoap">
  
<soap:address 
location
="http://localhost/
IFCEBrokerage/IFCEBrokerageFirm.asmx" 
/>
  
</port> </definitions>



The WSDL file defines its own namespace. WSDL adds a targetNamespace attribute to the <definitions> element. The targetNamespace attribute may not use a relative URI. Conversely, it must use the fully qualified URL. The namespace allows the author to qualify references to entities contained within the WSDL document.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

Referencing the Namespace

By doing this, the WSDL file assigns the prefix wsdlns to reference the namespace. This prefix fully qualifies all references to entities combined within the document. In addition, it sets prefixes for the <types> element as well as the XSD Schema. The <definitions> element sets boundaries for a specified name. All elements declared within the WSDL file define entities for messages and ports. Assign entities utilizing the Name attribute.

In a production environment, it is imperative to change the target namespace to your own. The temporary namespace displayed in the WSDL document, for example,


targetNamespace="http://tempuri.org/"
  
xmlns
=http://schemas.xmlsoap.org/wsdl/



specifies the http://tempuri.org namespace. It is better to do the following:


targetNamespace="http://IFCEBrokerageFirm.com/"
  
xmlns
="http://schemas.xmlsoap.org">

Returning to Listing 10, the <types> element contains XSD Schema information contained within the WSDL file. For example, examine the following document segment, and observe how the schema element appears as an immediate child of <types>. In addition, <types> functions as a container for XSD Schema complex types.


<s:element 
name
="StockQuote">
 
<s:complexType>
  
<s:sequence>
   
<s:element minOccurs="0" maxOccurs="1" 
name="symbol"
    
type
="s:string"/>
  
</s:sequence>
 
</s:complexType>
</s:element>

The same is true for StockQuoteResponse. Its type is s:double.
The <message> element contains two parts, the message name and the part name. The message name is StockQuoteSOAPIn. The part name is parameters, and the element is s0:StockQuote. Notice that the message segment contains both a


<message name="StockQuoteSoapIn">

and a


<part name="parameters" element="s0:StockQuote" 
/>
</message>
<message 
name
="StockQuoteSoapOut">

  • The <message> element contains descriptions for logical elements of messages.
  • The <part> represents a parameter passed to the method.
  • The <portType> element defines a list of operations, each individually assigned to a specified <operation> child element. In this case, SecuritiesExchange SOAP is the portType name. The <operation> name is StockQuote. The input and output messages are

s0:StockQuoteIn


and

s0:StockQuoteOut

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

Implementing Interfaces

Recall that an interface is made up of a set of signatures, which in turn are made up of an operation’s name, the objects that it accepts as parameters, and its return values. The interface represents the entire set of requests that are sent to the interface. For example, by typing an interface, that is, Type IFCE, the interface accepts all requests for operations defined in that interface. Two objects representing the type can share individual parts of an interface. In addition, interfaces can contain other interfaces, referred to as subsets. Frequently, we can refer to a subtype as inheriting from its supertype interface.

Interfaces are fundamental to object-oriented programming. An interface represents an abstract concept by declaring its intentions, but never reveals the how. In essence, the interface says nothing about its implementation. This is a win-win situation for developers. They can mutually share the same interface and write totally unique
implementations without conflict, as long as they adhere to the interface’s signature.

Dynamic Binding
Interfaces support dynamic binding for disparate objects sharing the same signature. For example, developers can submit a request to an object with confidence, knowing it will accept the request because it supports object substitutability. One object containing the same interface is interchangeable with another object at runtime, courtesy of a mechanism called polymorphism. As long as the object’s signature is adhered to, the object will process any request with equanimity.

During component-based development, you construct the interface so a client invocation on a specified service will never know how the request is implemented. Encapsulation shields the implementation from the client. Therefore, you can think of an interface as having two parts:
  • Interface Defines a set of public method signatures but provides no implementations
  • Published interface A uniquely identifiable interface made available through a registry to clients for dynamic discovery

Interfaces declared in Java or managed C++ typically provide only method signatures. Therefore, they describe why but not how the interface should be implemented. This allows for multiple implementations and allows services or components to enhance both
flexibility and scalability. However, the stateless Internet environment does not guarantee that the interface’s implementation is always secure and compliant with any behavioral specification. Despite this limitation, businesses are gravitating toward more service-oriented systems. Perhaps it is possible to adhere to the safe practice of defining a behavioral interface.

Objects are created by instantiating a class. This process allocates storage for the object’s internal data consisting of instance variables. This in turn binds the object with its variable types. By employing a methodology called class inheritance, the subclass inherits all parent data type definitions and its behavioral characteristics. This means objects will perform operations defined in both the parent and child
classes.

The sole purpose of an abstract class is to define a common interface for its child subclasses. However, an instance of this class can never be instantiated. Operations defined by an abstract class are called abstract operations, whereas classes that are not declared as abstract are called concrete classes. Abstract classes describe general
behavioral characteristics, whereas concrete classes define more specific operations on an interface’s signature.

Class Inheritance vs. Interface Inheritance

It is important to distinguish between an object’s class and its type. A class defines an object’s internal state and provides information on how to implement it. For example, superclass Employer is inheritable by several subclass employee types. A salaried employee defines its implementation in reference to class Employer. An employee
retained on commission defines its behavior in reference to class Employer, and so on. In essence, class inheritance is just a mechanism for implementing and extending a specified class’s functionality.

To be a bit banal, a child inherits all genetic characteristics from his/her parents, but extends the parents’ genetic and social inclinations by building on them and becoming an individual person. Interface inheritance reduces implementation dependencies between subsystems and components. Therefore, it is safe to assume that programming to an interface is better than programming to an implementation. Best practices suggest it is better to declare variables that are instances of an abstract class rather than instances of a concrete class.

In conclusion, perhaps one of the most important concepts is learning how to design reusable components. In addition, knowing how to implement abstract interfaces is essential to designing web-based applications in ASP.NET. Remember, the .NET Framework and the ASP.NET runtime are composed of reusable components and interfaces. Therein lies the key to understanding .NET and ASP.NET.

Remember: This is part one of chapter 7 .NET & J2EE Interoperability, by Dwight Peltzer (McGraw-Hill/Osborne, ISBN 0-07-223054-1, 2004). If you like what you see, feel free to click on the following link to get your own copy!
Buy this book now.

 

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials