Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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 
Mobile Linux 
App Generation ROI 
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 - Applying the ServiceContract attribute


    (Page 4 of 4 )

    WCF lets you apply the ServiceContract attribute on an interface or on a class. When you apply it on an interface, some class needs to implement the interface. In general, you use plain C# or VB to implement the interface, and nothing in the service class code pertains to it being a WCF service:

      [ServiceContract]
     
    interface IMyContract
      {
        
    [OperationContract]
        
    string MyMethod();
      }
      class MyService : IMyContract
      {
        
    public string MyMethod()
         {
            return "Hello WCF";
         }
      }

    You can use implicit or explicit interface implementation:

      class MyService : IMyContract
      {
         string IMyContract.MyMethod()
         {
           
    return "Hello WCF";
         }
      }

    A single class can support multiple contracts by deriving and implementing multiple interfaces decorated with theServiceContractattribute.

      [ServiceContract]
      interface IMyContract
      {
        
    [OperationContract]
        
    string MyMethod();
      }
      [ServiceContract]
      interface IMyOtherContract
      {
         [OperationContract]
        
    void MyOtherMethod();
     
    }

      class MyService : IMyContract,IMyOtherContract
     
    {
         public string MyMethod()
        
    {...}
        
    public void MyOtherMethod()
        
    {...}
     
    }

    There are, however, a few implementation constraints on the service implementation class. You should avoid parameterized constructors because only the default constructor will ever be used by WCF. Also, although the class can use internal properties, indexers, and static members, no WCF client will ever be able to access them.

    WCF also lets you apply theServiceContractattribute directly on the service class, without ever defining a separate contract first:

      //Avoid
      [ServiceContract]
      class MyService
      {
        
    [OperationContract]
        
    string MyMethod()
        
    {
           
    return "Hello WCF";
         }
      }

    Under the covers, WCF will infer the contract definition. You can apply theOperationContract attribute on any method of the class, be it private or public.

    Avoid using theServiceContractattribute directly on the service class. Always define a separate contract, so that you can use it in other contexts.

    Names and namespaces

    You can and should define a namespace for your contract. The contract namespace serves the same purpose in WCF as it does in .NET programming: to scope a type of contract and reduce the overall chance for a collision. You use the Namespaceproperty of theServiceContractattribute to provide a namespace:

      [ServiceContract(Namespace = "MyNamespace")]
      interface IMyContract
      {...}

    Unspecified, the contract namespace defaults to http://tempuri.org. For outward-facing services you would typically use your company’s URL, and for intranet services you can use any meaningful unique name, such asMyApplication.

    By default, the exposed name of the contract will be the name of the interface used. However, you could use an alias for a contract to expose a different name to the clients in the metadata using theNameproperty of theServiceContractattribute:

      [ServiceContract(Name = "IMyContract")]
      interface IMyOtherContract
      {...}

    In similar manner, the name of the publicly exposed operation defaults to the method name, but you can use theNameproperty of theOperationContractattribute to alias it to a different publicly exposed name:

      [ServiceContract]
      interface IMyContract
      { 
       
     [OperationContract(Name = "SomeOperation")]
        
    void MyMethod(string text);
      }

    You will see a use for these properties in the next chapter.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT