BrainDump
  Home arrow BrainDump arrow Services and the WCF
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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? 
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


    Services and the WCF


    (Page 1 of 4 )

    In this fourth part of a ten-part series focusing on the Windows Communication Foundation (WCF), aka "Indigo," you'll learn how to create a proxy to invoke a service, how to define a service, and more. 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). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Creating a proxy to invoke a service

    Now you will create a new console application to test the service. To do this, the client requires metadata from the service and information about its endpoint. This information will be used to initialize a client proxy that can invoke service operations.

    1. Go to Solution Explorer and add a new Console Application to the solution. Name the new projectClient.
    2. As you might expect, this project also requires a reference toSystem.ServiceModel. Add this reference and add the followingusingstatement to Program.cs:

        using System.ServiceModel;
    3. Copy the service contract to the client. First, add a new class to theClientproject, naming the fileServiceProxy.cs. Open this new file in the code window and add theIHelloIndigoServicecontract metadata as shown in Example 1-2. This service contract supplies the necessary metadata to the client, describing namespaces and service operation signatures.
    4. Now you can add code to invoke the service endpoint. Open Program.cs and modify theMain()entry point by adding the code as shown in Example 1-3. This code uses theChannelFactoryto create a new channel to invoke the service. This strongly typed channel reference acts as a proxy. The code also initializes anEndpointAddresswith the correct address and binding expected by the service endpoint.
    5. Test the client and service. Compile the solution and run theHostproject first, followed by theClientproject. TheClient console output should look similar to that shown in Figure 1-18 .

    Example 1-2. Service contract metadata for the client

    using System.ServiceModel;

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

    Example 1-3. Code to invoke a service through its generated proxy

    static void Main(string[] args)
    {
      EndpointAddress ep = new
    EndpointAddress("http://localhost:8000/HelloIndigo/ HelloIndigoService");

      IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.
    CreateChannel(new BasicHttpBinding(), ep);
      string s = proxy.HelloIndigo();
      Console.WriteLine(s);
     
    Console.WriteLine("Press <ENTER> to terminate Client.");
      Console.ReadLine();
    }


    Figure 1-18.  Output from the Client application

    In the next few sections, I will explain in more detail the steps you completed and the features you explored in the lab.

    Assembly Allocation

    The first thing I'd like to touch on is the allocation of assemblies when you create a new solution that includes services. For example, in this lab you created a new solution with three projects: one for the service, another for the host, and another for the client. Note that the service definition is decoupled from the host project. This is an approach I always recommend because it allows you to host the same service in multiple environments. For example, you may need to expose a service behind the firewall over TCP, and yet also allow remote, interoperable clients to consume it over HTTP. These two approaches require distinct hosting environments (specifically, a Windows service and IIS, as I will discuss in Chapter 4 at length). For simplicity, many examples may couple service and host, but this is merely a convenience--not a practical approach. As such, at a minimum I recommend that you always create a separate project for service contracts and services.

    Services are the window through which business functionality is invoked, but business logic has no place in the service assembly. Business functionality should never be coupled with the service implementation because it is possible that multiple services and applications may need to reuse the same business logic. Furthermore, while you may use services to reach that functionality in most cases, what if you needed to expose an Enterprise Service component to interoperate with a particular application or system? If business logic is stored in its own assemblies, this type of sharing is made easy.

    Another reason to decouple business logic from service implementation is to improve manageability and versioning. The service tier may need to coordinate logging activities and exception handling around calls to business components, and the service tier may need to be versioned, while business components and associated functionality have not changed. For this reason, I always recommend that business components, data access components, and other dependencies of the business tier also represent a separate set of assemblies in your solution. Figure 1-19 illustrates this breakdown from a high level.


    Figure 1-19.  Assembly allocation for services, hosts, business components, and data access

    There may be times when it is desirable to share the service contracts with client applications. In that case, service contracts and service implementations may also be decoupled. This makes it possible to share the metadata of the service without sharing the implementation.

    Later in this chapter, you'll see a scenario in which the service contract and service are decoupled.

    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

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek