Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - WCF Essentials
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

WCF Essentials
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2007-09-20

    Table of Contents:
  • WCF Essentials
  • Addresses
  • Contracts
  • Applying the ServiceContract attribute

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    WCF Essentials - Contracts


    (Page 3 of 4 )

     

    In WCF, all services expose contracts. The contractis a platform-neutral and standard way of describing what the service does. WCF defines four types of contracts.

    Service contracts

    Describe which operations the client can perform on the service. Service contracts are the subject of the next chapter, but are used extensively in every chapter in this book.

    Data contracts

    Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such asintandstring, but you can easily define explicit opt-in data contracts for custom types. Chapter 3 is dedicated to defining and using data contracts, and subsequent chapters make use of data contracts as required.

    Fault contracts

    Define which errors are raised by the service, and how the service handles and propagates errors to its clients. Chapter 6 is dedicated to defining and using fault contracts.

    Message contracts

    Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format you have to comply with. As a WCF developer, you should use message contracts only rarely, so this book makes no use of message contracts.

    The Service Contract

    The ServiceContractAttributeis defined as:

      [AttributeUsage(AttributeTargets.Interface|AttributeTargets.Class,
                   
    Inherited = false)]
      public sealed class ServiceContractAttribute : Attribute
      {
        
    public string Name
        
    {get;set;}
        
    public string Namespace
        
    {get;set;}
        
    //More members
      }

    This attribute allows you to define a service contract. You can apply the attribute on an interface or a class, as shown in Example 1-1.

    Example 1-1. Defining and implementing a service contract

    [ServiceContract] 
    interface IMyContract 
    {
       
    [OperationContract] 
      
    string MyMethod(string text); 

       //Will not be part of the contract
       
    string MyOtherMethod(string text);
    }
    class MyService : IMyContract
    {
      
    public string MyMethod(string text)
      
    {
         
    return "Hello " + text;
      
    }
      
    public string MyOtherMethod(string text)
      
    {
         
    return "Cannot call this method over WCF";
       }
    }

    TheServiceContractattribute maps a CLR interface (or inferred interface, as you will see later on) to a technology-neutral service contract. TheServiceContractattribute exposes a CLR interface (or a class) as a WCF contract, independently of that type’s visibility. The type visibility has no bearing on WCF, because visibility is a CLR concept. Applying theServiceContractattribute on an internal interface exposes that interface as a public service contract, ready to be consumed across the service boundary. Without theServiceContractattribute, the interface is not visible to WCF clients, in line with the service-oriented tenet that service boundaries are explicit. To enforce that, all contracts must explicitly opt in: only interfaces (or classes) decorated with theServiceContractattribute will be considered as WCF contracts. Other types will not.

    In addition, none of the members of the type will ever be part of the contract when using theServiceContractattribute. You must explicitly indicate to WCF which methods to expose as part of the WCF contract using theOperationContractAttribute, defined as:

      [AttributeUsage(AttributeTargets.Method)]
      public sealed class OperationContractAttribute : Attribute
      {

         public string Name
        
    {get;set;}
        
    //More members
     
    }

    You can only apply theOperationContractattribute on methods, but not on properties, indexers, or events, which are CLR concepts. WCF only understands operations—logical functions—and theOperationContractattribute exposes a contract method as a logical operation to perform as part of the service contract. Other methods on the interface (or class) that do not have theOperationContractattribute will not be part of the contract. This enforces the explicit service boundary and maintains an explicit opt-in model for the operations themselves. In addition, a contract operation cannot use object references as parameters—only primitive types or data contracts are allowed.

    More Windows Scripting Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming WCF Services," published by...
     

    Buy this book now. This article is excerpted from chapter one of Programming WCF Services, written by Juval Lowry (O'Reilly, 2007; ISBN: 0596526997). Check it out today at your favorite bookstore. Buy this book now.

    WINDOWS SCRIPTING ARTICLES

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway