![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
| | #2 (permalink) |
| 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 |