Friday, January 20, 2012

WCF-4 Client/Host quick start/tutorial

For those of you who already know WCF basics and need to start quickly here are guidelines to start a working client-server/service project:

My Quickest Start
  1. Designing Service Contracts
    1. Start simple, grow in time.
  2. Implement the contract
  3. Host the service - Console (starting simple).
    1. http://msdn.microsoft.com/en-us/library/ms731758.aspx
      1. New 'Console Application'
      2. See 'Example Host Code' below...
        1. Add Reference.... Select System.ServiceModel
        2. Reference 'ServiceModel'
        3. Start the service code
          1. Using
          2. Open
          3. Close
  4. Disable WcfSvcHost (if needed)
    1. "Right-click the WCF Service Project in Solution Explorer, choose Properties, and click WCF Options tab. The Start WCF Service Host when debugging another project in the same solution check box is enabled by default."
  5. CLIENT SIDE - Obtain the Service Contract, Bindings, and Addresses
    1. Enable the service
      1. Set the Host as the default startup program and run in without debugger (usually <CTRL>+F5).
    2. 'Add Service Reference...' 
      1. Past the service address in the 'Address' field and 'Go'.
      2. Point the service and 'OK' =results=> a new client proxy code will be generated in your client.
    3. SET YOUR CLIENT APPLICATION AS THE STARTUP PROGRAM :)
    4. Add service usage code to your client (see 'Example Client Code' below...).
  6. Run and relax :-).

    Example Host Code
    Code Snippet
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. using System.ServiceModel;
    7. using System.ServiceModel.Description;
    8. using MyWcfLib;
    9.  
    10.  
    11. namespace MyServer
    12. {
    13.     class Program
    14.     {
    15.         static void Main(string[] args)
    16.         {
    17.             Uri baseAddress = new Uri("http://localhost:9090/hello");
    18.  
    19.             using (ServiceHost host = new ServiceHost(typeof(MyService), baseAddress))
    20.             {
    21.                 // Enable metadata publishing.
    22.                 ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
    23.                 smb.HttpGetEnabled = true;
    24.                 smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    25.                 host.Description.Behaviors.Add(smb);
    26.  
    27.                 // Open the ServiceHost to start listening for messages. Since
    28.                 // no endpoints are explicitly configured, the runtime will create
    29.                 // one endpoint per base address for each service contract implemented
    30.                 // by the service.
    31.                 host.Open();
    32.  
    33.                 Console.WriteLine("The service is ready at {0}", baseAddress);
    34.                 Console.WriteLine("Press <Enter> to stop the service.");
    35.                 Console.ReadLine();
    36.  
    37.                 // Close the ServiceHost.
    38.                 host.Close();
    39.             }
    40.  
    41.         }
    42.     }
    43. }

    Example Client Code
    Code Snippet
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5.  
    6. namespace ConsoleApplication1
    7. {
    8.     class Program
    9.     {
    10.         static void Main(string[] args)
    11.         {
    12.             var client = new ServiceReference1.MyServiceClient();
    13.  
    14.             Console.WriteLine(client.GenerateGuid().ToString());
    15.             client.Close();
    16.  
    17.             Console.ReadLine();
    18.         }
    19.     }
    20. }


    MSDN's 'Basic Programming Lifecycle'


    Please, post comments if you find errors.
    I'll fix them ASAP and others can enjoy a cleaner clode.

    Hope it helps...

    No comments:

    Post a Comment