简体   繁体   English

坚持在Windows服务中托管的WCF服务吗? (即一次实例化一次)

[英]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: Windows服务中托管有一个WCF服务,如下所示:

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

Where DataService is a WCF Service Contract in the solution. 其中DataService是解决方案中的WCF服务合同。

Several layers below the WCF service is a cache in a seperate assembly. WCF服务下方的几层是单独程序集中的缓存。 But, every time a new connection/proxy to the WCF service is made, a new instance of the service is created. 但是,每次与WCF服务建立新的连接/代理时,都会创建该服务的新实例。 This results in a new instance of the cache being created in the DAL. 这导致在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). 因此,我想做的是拥有WCF服务,因此堆栈中的所有类都只能被实例化一次(由于多重性要求,某些情况除外)。 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. 因此,应该实例化WCF服务并侦听新的连接,而不是在每次建立新连接时都实例化DataService。

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. 我认为alexdej的答案是正确的,但是没有看到您的代码无法评论为什么会得到Null引用异常。

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. 不过,我可以将您指向这些视频,我仅在2天前完成了这些视频以帮助学习WCF,并且我相信他们会回答您的问题。

Self hosting WCF - http://channel9.msdn.com/shows/Endpoint/Endpoint-Screencasts-Self-hosting-WCF-Services/ 自托管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/ 将WCF托管为Windows服务-http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Hosting-WCF-Services-in-Windows-Services/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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