BrainDump
  Home arrow BrainDump arrow Page 2 - Multiple Service Contracts and Indigo
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 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

Multiple Service Contracts and Indigo
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-05-08

    Table of Contents:
  • Multiple Service Contracts and Indigo
  • Hosting two services with multiple contracts
  • Consuming internal services using shared contracts
  • Consuming external services with a generated 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


    Multiple Service Contracts and Indigo - Hosting two services with multiple contracts


    (Page 2 of 4 )

    Now you will host both services in a single console application. This will require you to create two ServiceHost instances and provide two <service> configuration sections, one for each service type.

    1. First, make sure theHost project can access the service contracts and service types. Go to theHostproject and add assembly references to two projects:BusinessServiceContractsandBusinessServices.
    2. In the application configuration file provided for theHost, provide configuration settings for both services. Open the app.config file and add the<system.serviceModel>section shown in Example 1-19. This section belongs inside the<configuration>section of the file.

      The configuration section forServiceAexposes two endpoints for the service contractIServiceA: one for Internet access over HTTP, another for TCP access behind the firewall.ServiceBalso exposes two endpoints for the service contractIServiceB: one for Internet access and another for named pipe access restricting communications to the same machine.

      Both services expose theIAdmincontract over TCP and named pipes, respectively, allowing callers on the same machine, or on remote machines behind the firewall. They also expose mex endpoints and enable the service metadata behavior for proxy generation.

      Each service configuration also provides the appropriate base addresses for the protocols they support across all endpoints.

      Example 1-19. Service model configuration for ServiceA and ServiceB

      <system.serviceModel>
        <services>
          <service name="BusinessServices.ServiceA" behaviorConfiguration="serviceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000"/>
                <add baseAddress="net.tcp://localhost:9000"/>
              </baseAddresses>
            </host>
            <endpoint address="Admin" contract="BusinessServiceContracts.IAdmin"
      binding="netTcpBinding" />
            <endpoint address="ServiceA" contract="BusinessServiceContracts.IServiceA"
      binding="basicHttpBinding" />
            <endpoint address="ServiceA" contract="BusinessServiceContracts.IServiceA"
      binding="netTcpBinding" />
          </service>
          <service name="BusinessServices.ServiceB" behaviorConfiguration="serviceBehavior">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8001"/>
                <add baseAddress="net.pipe://localhost"/>
              </baseAddresses>
            </host>
            <endpoint address="Admin" contract="BusinessServiceContracts.IAdmin"
      binding="netNamedPipeBinding" />
            <endpoint address="ServiceB" contract="BusinessServiceContracts.IServiceB"
      binding="basicHttpBinding" />
            <endpoint address="ServiceB" contract="BusinessServiceContracts.IServiceB"
      binding="netNamedPipeBinding" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="serviceBehavior">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>


      Each<service>section holds configuration settings for its own base addresses and endpoints. Recall that the configuration for a particular service is used to initialize aServiceHostinstance for that service type. Be mindful that base addresses across all sections must have unique ports since a port can be opened only once per machine.
    3. Now that service model configuration has been provided for each service, you will write code to initialize and open aServiceHostinstance for both. Go to theHostproject and open Program.cs. Modify theMain()entry point so that itincludes the code shown in Example 1-20. You will also need to add ausing statement forSystem.ServiceModel.

      This code creates two distinctServiceHost instances, one for each service. They are both constructed and opened within a try...finally block to ensure thatClose()is called for each when the host shuts down or if a fatal exception occurs.

      Example 1-20. Initializing the ServiceHost for ServiceA and ServiceB

      using System.ServiceModel;

      static void Main(string[] args)
      {
        ServiceHost hostA = null;
        ServiceHost hostB = null;

        try
        {
          hostA = new ServiceHost(typeof(BusinessServices.ServiceA));
          hostB = new ServiceHost(typeof(BusinessServices.ServiceB));

          hostA.Open();
          hostB.Open();

          Console.WriteLine();
          Console.WriteLine("Press <ENTER> to terminate Host");
          Console.ReadLine();
        }
        finally
        {
          hostA.Close();
          hostB.Close();
        }
      }

    4. Compile and run theHostproject once to verify that no errors occur.

    Because metadata browsing is enabled in the configuration section for each service type, you can browse to the WSDL document for each service by providing the HTTP base address for each service. Note that each service has its own distinct WSDL document, but for each individual service, all endpoints for the service are included in its WSDL document.

    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

    - Internet Explorer 8 Review
    - Nilpo`s Top Windows Add-Ons
    - Beginning Silverlight 2.0 Development using ...
    - Fixing Vista`s Troubles
    - Preparing Windows Images for Mass Deployment
    - The Trouble With Vista
    - Slipstreamed and Unattended Windows Installa...
    - Microsoft Office SharePoint Server
    - Microsoft Office SharePoint Designer
    - Microsoft Windows SharePoint Services 3.0
    - Microsoft Live Mesh Overview
    - XAML Brushes and Silverlight
    - Silverlight and XAML Basics
    - Immortal XP
    - XAML Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT