简体   繁体   中英

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

自己实例化DataService并将实例传递给ServiceHost构造函数:

sHost = new ServiceHost(new DataService());

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM