BrainDump
  Home arrow BrainDump arrow Page 2 - Services and the WCF
Iron Speed
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 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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? 
BRAINDUMP

Services and the WCF
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-03

    Table of Contents:
  • Services and the WCF
  • Defining a Service
  • Exposing Service Endpoints
  • Creating a Client Proxy

  • 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

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Services and the WCF - Defining a Service
    (Page 2 of 4 )

    The first step in creating a service is to define a service contract. You create a service contract by applying the ServiceContractAttribute to an interface or type. Methods on the interface or type will not be included in the service contract until the OperationContractAttribute is applied. In a typical service contract, all methods will be included in the contract--after all, the entire reason for defining a service contract is to expose operations as part of a service. Business interfaces should not be directly converted into service contracts. Likewise, business components should not be directly converted to services. The service tier should instead be explicitly defined with the sole purpose of exposing public functionality and should internally consume business components, rather than embed business logic with the service implementation.

    When you implement a service contract as an interface, the service type implements this interface. In this lab, the service implements a single service contract,IHelloIndigoService. This contract exposes a single operation,HelloIndigo().

    An alternative to this approach is to apply both theServiceContractAttributeand theOperationContractAttributedirectly to the service type. Example 1-4 shows the changes you would make to the lab to achieve this. Here is a summary of those changes:

    1. When you apply theServiceContractAttributeto the service type, the service type name becomes the official name of the service contract. Thus, this is the name that must be provided when you create a new endpoint (seeAddServiceEndpoint()).
    2. On the client side, the service contract can still be represented as an interface (the client only requires metadata) but the name of that interface (the service contract) must match the new service contract name,HelloIndigoService--instead ofIHelloIndigoService. To update the lab, you can rename the interface at the client, or specify a value for theName property of theServiceContactAttributeas shown in Example 1-4. Service contracts are discussed in Chapter 2.

    The following sample illustrates the coupling of service contracts with service type:<YourLearningWCFPath>\Sample\ServiceContracts\ ServiceContractOnServiceType.

    Example 1-4. Changes that support defining the service contract with the service type

    // HelloIndigo Project Service.cs

    [ServiceContract(Namespace = http://www.thatindigogirl.com/samples/ 2006/06)]
    public class HelloIndigoService
    {
      [OperationContract]
     
    public string HelloIndigo()
      {
        return "Hello Indigo";
      }
    }
     

    // Host Project - Program.cs

    host.AddServiceEndpoint(typeof(HelloIndigo.HelloIndigoService), new BasicHttpBinding(),
    "HelloIndigoService");

    // Client Project - ServiceProxy.cs

    [ServiceContract(Name="HelloIndigoService", Namespace = http://www.thatindigogirl.com/ samples/2006/06)]
    public interface IHelloIndigoService
    {
      
    [OperationContract]
       string HelloIndigo();
    }

    Hosting a Service

    Any managed process can host services. Within that process, you can create one or more ServiceHost instances, each associated with a particular service type and exposing one or more endpoints for that type. This lab shows you how to host a service by creating an instance of the ServiceHost type for the HelloIndigoServicetype within a console application.

    Before opening theServiceHostinstance, you can also provide it with base addresses if you are planning to create relative endpoints. In order to reach the service, at least one endpoint is required. To programmatically supply base addresses to theServiceHost, you can pass them to the constructor. TheServiceHosttype also provides anAddServiceEndpoint()method to create endpoints as shown here (from Example 1-1):

      using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService),
      new Uri(http://localhost:8000/HelloIndigo)))
      {
       
    host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
      new BasicHttpBinding(), "HelloIndigoService");

        // other code
      }

    In a simple scenario, theServiceHostneed only know its service type and associated endpoints where the service can be reached. This information is used to create a server channel that can receive and process messages. The channel is created when you call theOpen()method on theServiceHost instance. This creates a channel listener to receive messages for the service through its associated endpoints. The receiving channel processes incoming messages, invokes service operations, and processes responses. When theClose()method is called, the channel is gracefully disposed of after processing any remaining requests. In Example 1-1,Close()is automatically called when code block associated with theusingstatement ends.

    The using statement can be applied to any type that implementsIDisposable. At the end of the using statement,Dispose()is called within atry...finallyblock to ensure cleanup even in the case of an exception.

    More BrainDump Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning WCF A Hands-on Guide," published...
     

    Buy this book now. This article is excerpted from chapter 1 of the book Learning WCF A Hands-on Guide, written by Michele Leroux Bustamante (O'Reilly, 2007; ISBN: 0596101627). Check it out today at your favorite bookstore. Buy this book now.

    BRAINDUMP ARTICLES

    - Handling Multiple Contracts with Indigo
    - Cleaning Out Your Data in XP
    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager
    - Generating Clients and Services with Indigo
    - Vista SP1, A Review
    - Services and the WCF
    - VBScript: Final Date Functions
    - Creating Services with the WCF
    - The Resource View of the MFC

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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