BrainDump
  Home arrow BrainDump arrow Page 3 - Creating Services with 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

Creating Services with the WCF
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-27

    Table of Contents:
  • Creating Services with the WCF
  • Channels
  • Creating a New Service from Scratch
  • Hosting a service

  • 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


    Creating Services with the WCF - Creating a New Service from Scratch


    (Page 3 of 4 )

    You're about to be introduced to the WCF service. This lab isn't your typical "Hello World"--it's "Hello Indigo"! In this lab, you will learn how to build a new WCF service and in the process learn the minimum requirements of service development and consumption. Here's a short list of things you'll accomplish:

    1. Create a new service contract and service implementation
    2. Programmatically configure a service host, its endpoints, and bindings
    3. Create a client application and open a client channel proxy to invoke the service

    Now, before you start thinking "been there, done that," this simple lab will be slightly different because I'm going to give you some practical design tips that ensure configurability and appropriate decoupling of service, host, and client. In addition, I'll be diving deeper into basic concepts such as services, service contracts, endpoints, bindings,ServiceHost, and channels.


    Lab: Creating Clients and Services Programmatically


    In this first lab, you will create a new solution with three projects: a service, a host, and a client. When you run the service host, you'll expose a single service endpoint. The client application will access service operations through that endpoint. You'll host the service in a console application and invoke the service using a manually constructed proxy. This lab will teach you the basic requirements for creating, hosting, and consuming a service with WCF.

    Creating a new service

    The first thing you will do is create a new service contract with a single operation and implement this contract on a new service type.

    1. In this lab, everything begins from scratch, so you'll start by creating a new Visual Studio solution. Open a new instance of Visual Studio 2005. Select File -> New -> Project, and from the New Project dialog, create a new Blank Solution in the <YourLearningWCFPath>\Labs\Chapter1 directory. Name the solutionServiceFromScratch. Click OK to create the empty solution.
    2. Create the service project. From Solution Explorer, right-click on the solution node and select Add -> New Project. Select the Class Library template, name the projectHelloIndigo, and make sure the location path matches the solution at <YourLearningWCFPath>\Labs\Chapter1\ServiceFromScratch. Click OK to create the new project.
    3. Now you will create your first service contract. From Solution Explorer, rename the project's only class file to Service.cs. Open this file in the code window.

      Add a new interface namedIHelloIndigoServicein Service.cs. Add a single method to the interface,HelloIndigo, with the signature shown here:

        public interface IHelloIndigoService
        {
           string HelloIndigo();
        }

    4. Add a reference to theSystem.ServiceModelassembly. From Solution Explorer, right-click References and selectSystem.ServiceModelfrom the list. You'll also need to add the followingusingstatement to Service.cs:

        using System.ServiceModel;
    5. To turn this interface into a service contract, you'll need to explicitly decorate the interface with theServiceContractAttribute. In addition, each method should be decorated with theOperationContractAttributeto include it in the service contract. In this case, you'll makeIHelloIndigoServicea service contract and exposeHelloIndigo()as its only service operation by applying these attributes as shown here:

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


      Providing a namespace for theServiceContractAttributereduces the possibility of naming collisions with other services. This will be dicussed in greater detail in Chapter 2.
    6. In the same file, create a service type to implement the service contract. You can modify the existing class definition, renaming it toHelloIndigoService. Then add theIHelloIndigoServiceinterface to the derivation list and implementHelloIndigo()with the following code:

        public class HelloIndigoService : IHelloIndigoService
        {
           public string HelloIndigo()
           {
              return "Hello Indigo";
           }
        }

    7. Compile the service project.

    At this point, you've created a service contract with a single operation and implemented it on a service type. The service is complete at this point, but to consume it from a client application, you will need to host it first.

    More BrainDump Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning WCF A Hands-on Guide," published...
       · I created the WCF service on Windows Vista. As you may have known, Vista has the...
     

    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 5 Hosted by Hostway
    Stay green...Green IT