Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Indigo

Vista - What's the best way to manage client/proxy instances?

 
 
Old 08-22-2007   #1 (permalink)
Wol


 
 

What's the best way to manage client/proxy instances?

What is the best way to manage instancing on the client so that it's (a)
efficient, (b) doesn't get in the way of the calls you're trying to make and
(c) resilient to the timeouts on the server?



I've seen a lot of sample code that pays no attention to instancing and just
creates a new proxy any time it needs to call a service:




MyContractClient proxy = new MyContractClient("MyEndpoint");

proxy.GetOrders("tom");

proxy.Close();



According to the recently released .NET StockTrader appliction, this kind of
instancing can really hurt performance. It suggests a better way to work is
to save and reuse a single instance:




static object _channelLock = new object();

private static ChannelFactory _channelFactory;

private static IMyContract _channel;



public IMyContract Channel

{

get

{

IMyContract local = _channel;



if (local == null)

{

lock (_channelLock)

{

if (_channelFactory == null)

{

_channelFactory = new ChannelFactory("tcp"); // NOTE: TCP binding

}

if (_channel == null)

{

_channel = _channelFactory.CreateChannel();

}

return _channel;

}

}

return local;

}

set

{

lock (_channelLock)

{

if (((IChannel)_channel).State != CommunicationState.Opened)

{

((IChannel)_channel).Abort();

_channel = null;

if (_channelFactory.State != CommunicationState.Opened)

{

_channelFactory.Abort();

_channelFactory = null;

}

}

}

}

}



In this code, the public Channel property hides an instance of a WCF channel
and its associated channel factory. The StockTrader application uses the
Channel property like this:




public class MyService

{

public IMyContract Channel

{

// as above

}



public List GetOrders(string userId)

{

try

{

return Channel.GetOrders(userId);

}

catch

{

Channel = null;

throw;

}

}

}



That seems to satisfy (a) efficiency and (b) doesn't get in the way of the
calls you're trying to make.



I thought it might satisfy (c) too until I tried doing this:



MyService service = new MyService();



List list1 = Channel.GetOrders("tom");



Console.Read(); // pause until a key is pressed



List list2 = Channel.GetOrders("sam");





Before I pressed a key to continue, I went to get a cup of coffee. When I
came back 11 minutes later, I hit a key and dropped through to the second
GetOrders call which immediately threw a CommunicationException because while
I'd been away, the service had timed out (a receive timeout) and closed the
connection. (More about this here.)



So, what is the best way to manage instancing on the client so that it's (a)
efficient, (b) doesn't get in the way of the calls you're trying to make and
(c) resilient to the timeouts on the server?




thanks


My System SpecsSystem Spec
Old 08-24-2007   #2 (permalink)
Sherif Elmetainy


 
 

Re: What's the best way to manage client/proxy instances?

Greetings

To solve the timeout problem you can change the closeTimeout, sendTimeout,
receiveTimeout and openTimeout properties in you client's binding
configuration. You can increase the values for these properties depending on
the how long the service takes to perform the operation and the speed of the
connection between the client and the service. If the service is expected to
take a very long time to perform an operation, you might want to consider
having the service perform the operation asynchronously.

For example
<wsHttpBinding>
<binding name="MyBinding" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" >
<!-- other binding configurations here -->
</binding>
</wsHttpBinding>

Regards,
Sherif

"Wol" <Wol@discussions.microsoft.com> wrote in message
news:AD7D9BBC-A92D-4F1B-8383-93F4BAF29FBB@microsoft.com...
> What is the best way to manage instancing on the client so that it's (a)
> efficient, (b) doesn't get in the way of the calls you're trying to make
> and
> (c) resilient to the timeouts on the server?
>

......


My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Vista client under squid proxy Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46