|
-
March 11th, 2011, 04:24 AM
#1
WCF - sharing a Dictionary<T> between multiple processes
Hi,
I have an application that populates a couple of generic dictionaries (via calls to a web service) when it starts up.
This works fine when there is only one instance of the application running.
I run into problems as soon as there are more than one instances running at the same time.
The second and subsequent instances always timeout while populating those dictionaries.
So I'm thinking about trying to have the first instance populate the dictionaries and provide some method for any subsequent instances to search and add items to them.
As far as I can tell WCF will cater for this type of scenario, but I would like to get some confirmation/denial from people who know more about WCF than I do (I have never used it).
The general run of things would be:
1. 1st instance is started and it immediately starts filling the dictionaries
2. 2nd, 3rd, 4th instances are started, they will read from and add items to the dictionaries
3. The dictionaries need to be available for as long as any instances of the application are running
Is this possible using WCF? Any suggestions on how I might go about this? I'm not looking for code (not yet anyway, I'm sure I'll have problems along the way), more like a general overview on how to proceed.
Thanks,
David
-
March 11th, 2011, 10:42 AM
#2
Re: WCF - sharing a Dictionary<T> between multiple processes
By default, when a WCF request is make a new instance of the service is created. So if you created a dictionary as a class member of the service class, you would have separate dictionaries for every request.
To avoid this problem, you can put the dictionary into a singleton class (and access this class from your service class). That way there would be only one dictionary used across all the service calls.
Another way to approach this is to set InstanceContextMode property of the ServiceBehavior attribute.
Code:
[ServiceBehavior( InstanceContextMode = InstanceContextMode.Single )]
public class MyWcfService : IWcfService
{
...
}
A word of warning... Because the dictionary will be shared between multiple threads (from multiple process requests), you'll need to synchronize access to the dictionary with a lock or critical section.
I've written a Tray Notify article series that uses Wcf to share data between processes (on the same machine or across the network). You might find it interesting.
http://www.codeguru.com/csharp/.net/...n---Part-I.htm
Part III talks about Wcf. Part I gets you going and Part II talks about hosting the Wcf service inside a Windows Service application.
Last edited by Arjay; March 14th, 2011 at 01:12 PM.
-
March 14th, 2011, 07:51 AM
#3
Re: WCF - sharing a Dictionary<T> between multiple processes
Thanks very much Arjay - I'll have a look at those articles.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|