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>
Next: Programmatic Client Configuration >>
More Windows Scripting Articles
More By O'Reilly Media
|
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.
|
|