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

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

    Table of Contents:
  • WCF and Proxies
  • Programmatic Client Configuration
  • WCF Architecture
  • The InProcFactory Class
  • Binding and Reliability
  • Requiring Ordered Delivery

  • 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 Proxies


    (Page 1 of 6 )

    In this final article in a five-part series on using the Windows Communication Foundation (WCF), you will learn how to create and use a proxy, how to programmatically configure a client, and more. It is excerpted from chapter one of Programming WCF Services, written by Juval Lowry (O'Reilly, 2007; ISBN: 0596526997). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Creating and Using the Proxy

    The proxy class derives from the class ClientBase<T>, defined as:

      public abstract class ClientBase<T> : ICommunicationObject,IDisposable
      {
         protected ClientBase(string endpointName);
         protected ClientBase(Binding binding,EndpointAddress remoteAddress);
         public void Open();
         public void Close();
         protected T Channel
         {get;}
         //Additional members 
     
    }

    ClientBase<T>accepts a single generic type parameter identifying the service contract that this proxy encapsulates. TheChannel property ofClientBase<T>is of the type of that type parameter. The generated subclass ofClientBase<T>simply delegates toChannelthe method call (see Example 1-15).

    To use the proxy, the client first needs to instantiate a proxy object and to provide the constructor with endpoint information: either the endpoint section name from the config file, or the endpoint address and binding objects if you’re not using a config file. The client can then use the proxy methods to call the service, and when the client is done, the client needs to close the proxy instance. For example, given the same definitions as in Examples 1-15 and 1-16, the client constructs the proxy, identifying the endpoint to use from the config file; invokes the method; and closes the proxy:

      MyContractClient proxy = new MyContractClient("MyEndpoint");
      proxy.MyMethod();
      proxy.Close();

    If only one endpoint is defined in the client config file for the type of contract the proxy is using, then the client can omit the endpoint name from the proxy’s constructor:

      MyContractClient proxy = new MyContractClient();
     
    proxy.MyMethod();
      proxy.Close();

    However, if multiple endpoints are available for the same contract type then the proxy throws an exception.

    Closing the proxy

    It is a recommended best practice to always close the proxy when the client is done using it. You will see in Chapter 4 why the client needs to close the proxy in certain cases, because closing the proxy terminates the session with the service and closes the connection. 

    Alternatively, you can use theDispose()method of the proxy to close it. The advantage of theDispose()method is that you can use theusingstatement to call it even in the face of exceptions:

      using(MyContractClient proxy = new MyContractClient())
      { 
        
    proxy.MyMethod();
     
    }

    If the client is declaring the contract directly instead of the concrete proxy class, the client can either query for the presence of IDisposable:

      IMyContract proxy = new MyContractClient());
      proxy.MyMethod();
      IDisposable disposable = proxy as IDisposable;
      if(disposable != null)
      {
        
    disposable.Dispose();
      }

    or collapse the query inside theusingstatement:

      IMyContract proxy = new MyContractClient();
     
    using(proxy as IDisposable
      {
         proxy.MyMethod();
      }

    Call timeout

    Each call made by a WCF client must complete within a configurable timeout. If for whatever reason the call duration exceeds the timeout, the call is aborted and the client gets a TimeoutException. The exact value of the timeout is a property of the binding, where the default timeout is one minute. To provide a different timeout, set the SendTimeout property of the abstract Binding base class:

      public abstract class Binding : ...
      {
         public TimeSpan SendTimeout
         {get;set;}
         //More members
      }

    For example, when using theWSHttpBinding:

      <client>
         <endpoint
            ...
            binding = "wsHttpBinding"
            bindingConfiguration = "LongTimeout"
            ...
         />
      </client>
      <bindings>
        
    <wsHttpBinding>
            <binding name = "LongTimeout" sendTimeout = "00:05:00"/>
         </wsHttpBinding>
      </bindings>

    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 today at your favorite bookstore. Buy this book now.

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek