Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Indigo

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

 
 
Thread Tools Display Modes
Old 08-22-2007   #1 (permalink)
Wol
Guest


 

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

Old 08-24-2007   #2 (permalink)
Sherif Elmetainy
Guest


 

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?
>

......


 

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista client under squid proxy roberto Vista networking & sharing 0 03-23-2007 12:49 PM








Vistax64.com 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 2005-2008

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 47 48 49 50