Fugu
Fugu

Reputation: 361

Persisting a WCF service hosted within a windows service? (i.e. instantiating once and only once)

I have the following problem. There is a WCF service hosted within a windows service like this:

sHost = new ServiceHost(typeof(DataService));
_thread = new Thread(new ThreadStart(sHost.Open));
_thread.Start();

Where DataService is a WCF Service Contract in the solution.

Several layers below the WCF service is a cache in a seperate assembly. But, every time a new connection/proxy to the WCF service is made, a new instance of the service is created. This results in a new instance of the cache being created in the DAL. So what I would like to do is have the WCF service and therefore all classes down the stack instanced once and only once (with some exceptions due to multiplicity requirements). So, the WCF service should be instanced and listen for new connections, rather than have DataService instanced every single time a new connection is made.

I hope this makes sense. How do I do this?

Many thanks, Fugu

Upvotes: 0

Views: 171

Answers (2)

JonWillis
JonWillis

Reputation: 3147

I think alexdej answer is correct, but without seeing your code cannot comment to why you get a Null reference exception.

I can however point you to these videos', I completed these only 2 days ago to help learn WCF and I am sure they will answer your question.

Self hosting WCF - http://channel9.msdn.com/shows/Endpoint/Endpoint-Screencasts-Self-hosting-WCF-Services/

Hosting WCF as a windows service - http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Hosting-WCF-Services-in-Windows-Services/

Upvotes: 1

alexdej
alexdej

Reputation: 3781

Instantiate DataService yourself and pass the instance to the ServiceHost constructor:

sHost = new ServiceHost(new DataService());

Upvotes: 2

Related Questions