BrainDump
  Home arrow BrainDump arrow Page 3 - Generating Clients and Services with Indig...
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

Generating Clients and Services with Indigo
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-04-10

    Table of Contents:
  • Generating Clients and Services with Indigo
  • Configuring service endpoints using the Service Configuration Editor
  • Generating a proxy with Add Service Reference
  • Generating a proxy using the Service Model Metadata Utility

  • 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


    Generating Clients and Services with Indigo - Generating a proxy with Add Service Reference


    (Page 3 of 4 )

    It's time to generate code for the client to consume the service, starting by generating a client proxy. To achieve this you will use the Add Service Reference functionality exposed by Visual Studio, which uses the ServiceModel Metadata Utility (SvcUtil) to generate a proxy and configuration settings for that proxy.

    1. Go to theClient project and from Solution Explorer, right-click on theClientproject node and select Add Service Reference. The dialog presented requires you to provide a valid base address to the service. Supply the base address http:// localhost:8000/HelloIndigo and leave the Service reference name aslocalhost. When you close this dialog, a service proxy and configuration file will be generated for the client application. Stop debugging so you can add code to the client application.

      To see the proxy, go to theClientproject and expand the Service References folder. Beneath it you will see localhost.map, and beneath that localhost.cs--the latter of which contains the proxy.

      A new configuration file, app.config, was also added to the project. This contains the service model configuration for the proxy. Later I'll talk about how these things come together.
    2. Add code to the client application to invoke the service using the generated proxy. Go to theClientproject and open Program.cs. Add code to theMain()entry point as shown in bold in Example 1-6.
    3. Compile the solution and run theHostproject, followed by theClient. The client's console output should show the result of invoking the service'sHelloIndigo operation.

    Example 1-6. Using a generated proxy to invoke a service

    static void Main(string[] args)
    {
     
    localhost.HelloIndigoServiceClient proxy = new
    Client.localhost.HelloIndigoServiceClient()
      string s = proxy.HelloIndigo();
      Console.WriteLine(s);

      Console.WriteLine("Press <ENTER> to terminate Client.");
      Console.ReadLine();
    }

    This concludes one technique for generating a service,ServiceHostconfiguration, and a client proxy.

    Creating a WCF Service Library

    In this section, you will generate a service using another technique: adding a new class library that includes a WCF service. The WCF Service Library template is a quick and easy way to generate a new class library with a sample service contract, service type, and the appropriate assembly references.

    1. Go to the Solution Explorer and right-click on the solution node. Select Add -> New Project and select the WCF Service Library template. Name the projectHelloIndigo.
    2. Rename the class file from Class1.cs to HelloIndigoService.cs.
    3. Modify the service contract that is supplied by the project template. Open HelloIndigoService.cs in the code window and provide a namespace for theServiceContractAttributeand change the interface definition to look as follows:

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

    4. Now, modify the service implementation so that it implements the new contract. Rename the service toHelloIndigoServiceand implementIHelloIndigoServiceas shown here:

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


      Compile theHelloIndigoproject.

      The default data contract created when you used the WCF Service template is not necessary for this lab.
    5. Now you will modify the existing host project so that it hosts this new service. Go to theHostproject and open HelloIndigoService.cs. Comment the entire service contract and implementation to avoid collision with theHelloIndigolibrary you're about to reference.
    6. Add a reference to theHelloIndigoclass library project. Right-click on theHostnode and select Add Reference. From the Projects tab, select theHelloIndigoproject.
    7. TheServiceHostmust be modified to refer to the service type from this project. In the HelloIndigoService.cs file, find theStartService()method and modify theServiceHost constructor to use the fully qualified name of the service,HelloIndigo.HelloIndigoService, as shown here:

        myServiceHost = new ServiceHost(typeof(HelloIndigo.HelloIndigoService));
    8. You'll also have to edit the service model section of the configuration file to use the correct service and contract types. Go to theHost project and open the app.config file. Change the service type and contract type for the<service>configuration section as shown here in bold:

        <service behaviorConfiguration="serviceBehavior" name="HelloIndigo.
        HelloIndigoService">
          <endpoint address="HelloIndigoService" binding="basicHttpBinding" name="basicHttp" contract="HelloIndigo.IHelloIndigoService" />

        <!-- other settings -->

      </service>

    9. Test the solution again by compiling and running theHostand then theClient.

    Now you have learned how to create a new class library with a sample WCF service and seen the changes required to the service model configuration andServiceHostto reference a different service type.

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