Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - WCF and Bindings
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? 
WINDOWS SCRIPTING

WCF and Bindings
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-10-04

    Table of Contents:
  • WCF and Bindings
  • Endpoints
  • Binding configuration
  • Metadata Exchange

  • 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


    WCF and Bindings - Binding configuration


    (Page 3 of 4 )

    You can use the config file to customize the binding used by the endpoint. To that end, add the bindingConfiguration tag to the endpoint section, and name a customized section in thebindingssection of the config file. Example 1-8 demonstrates using this technique to enable transaction propagation. What thetransactionFlowtag does will be explained in Chapter 7.

    Example 1-8. Service-side binding configuration

    <system.serviceModel>
       <services>
          <service name = "MyService"> 
             <endpoint
                address  = "net.tcp://localhost:8000/MyService/"
               
    bindingConfiguration = "TransactionalTCP"
              
    binding  = "netTcpBinding"
                
    contract = "IMyContract"
             />
             <endpoint
                address  = "net.tcp://localhost:8001/MyService/"
               
    bindingConfiguration = "TransactionalTCP"
              
    binding  = "netTcpBinding"
                contract = "IMyOtherContract"
             />
          </service>
       </services>
      
    <bindings>
          <netTcpBinding>
             <binding name = "TransactionalTCP"
                transactionFlow = "true"
             />
           </netTcpBinding>
        </bindings>
    </system.serviceModel>

    As shown in Example 1-8, you can reuse the named binding configuration in multiple endpoints simply by referring to it.

    Programmatic Endpoint Configuration

    Programmatic endpoint configuration is equivalent to administrative configuration. Instead of resorting to a config file, you rely on programmatic calls to add endpoints to the ServiceHost instance. Again, these calls are always outside the scope of the service code. ServiceHost provides overloaded versions of theAddServiceEndpoint()method:

      public class ServiceHost : ServiceHostBas
      {
         public ServiceEndpoint AddServiceEndpoint(Type implementedContract,
                       Binding binding,
                       string address);
         //Additional members
      }

    You can provideAddServiceEndpoint()methods with either relative or absolute addresses, just as with a config file. Example 1-9 demonstrates programmatic configuration of the same endpoints as in Example 1-7.

    Example 1-9. Service-side programmatic endpoint configuration

    ServiceHost host = new ServiceHost(typeof(MyService));

    Binding wsBinding  = new WSHttpBinding(); Binding tcpBinding = new NetTcpBinding();

    host.AddServiceEndpoint(typeof(IMyContract),wsBinding,  
              "http://localhost:8000/MyService"); host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, 
          "net.tcp://localhost:8001/MyService"); host.AddServiceEndpoint(typeof(IMyOtherContract),tcpBinding, 
          "net.tcp://localhost:8002/MyService");

    host.Open();

    When you add an endpoint programmatically, the address is given as a string, the contract as aType, and the binding as one of the subclasses of the abstract classBinding, such as:

      public class NetTcpBinding : Binding,...
      {...}

    To rely on the host base address, provide an empty string if you want to use the base address, or just the URI to use the base address plus the URI:

      Uri tcpBaseAddress = new Uri("net.tcp://localhost:8000/");

      ServiceHost host = new ServiceHost(typeof(MyService),tcpBaseAddress);

      Binding tcpBinding = new NetTcpBinding();

      //Use base address as address
      host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"");
      //Add relative address
      host.AddServiceEndpoint(typeof(IMyContract),tcpBinding,"MyService");
      //Ignore base address
      host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, 

      "net.tcp://localhost:8001/MyService");
      host.Open();

    As with administrative configuration using a config file, the host must provide a matching base address; otherwise, an exception occurs. In fact, there is no difference between programmatic and administrative configuration. When you use a config file, all WCF does is parse the file and execute the appropriate programmatic calls in its place.

    You can programmatically set the properties of the binding used. For example, here is the code required to enable transaction propagation similar to Example 1-8:

      ServiceHost host = new ServiceHost(typeof(MyService));

      NetTcpBinding tcpBinding = new NetTcpBinding();

      tcpBinding.TransactionFlow = true;

      host.AddServiceEndpoint(typeof(IMyContract),tcpBinding, 
          "net.tcp://localhost:8000/MyService");
      host.Open();

    Note that when you’re dealing with specific binding properties, you typically interact with a concrete binding subclass such asNetTcpBinding, and not its abstract base classBindingas in Example 1-9.

    More Windows Scripting Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming WCF Services," published by...
     

    Buy this book now. This article is excerpted from chapter one of Programming WCF Services, written by Juval Lowry (O'Reilly, 2007; ISBN: 0596526997). Check it out at your favorite bookstore. Buy this book now.

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





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