![](/img/trans.png)
[英]How to call a method of the ServiceHost from the hosting process in WCF C#
[英]C# -> Windows service -> WCF ServiceHost -> ? How to implement long running process
我正在開發一項服務,該服務可以24/7運行,並可以通過MVC網站進行控制,但這並不是那么重要。 不過重要的是,我現在擁有的是(對於Windows服務):
protected override void OnStart(string[] args)
{
if (this.ServiceHost != null)
this.ServiceHost.Close();
this.ServiceHost = new ServiceHost(typeof(ParserService));
this.ServiceHost.Open();
}
目前,ParserService如下:
public class ParserService : IParserService
{
private ParserFacade FacadeForParser;
public void Start()
{
if(FacadeForParser == null)
this.FacadeForParser = new ParserFacade();
this.FacadeForParser.Start();
}
public bool IsRunning()
{
return true;
}
public void Stop()
{
this.FacadeForParser.Stop();
}
public List<string> GetAllTitles()
{
return this.FacadeForParser.GetAllTitles();
}
}
問題是:在解析器服務中,FacadeForParser永遠不會占用下一個調用。 好像我誤解了WCF類的生命周期...所以有人可以告訴我實現此部分的正確方法嗎?
我確實需要:1個ParserFacade實例,以通過該服務的WCF部分處理過的所有請求來使用它。
我已經找到這種情況的解決方案。 這不是我最初的想法,但結果正是我想要的。
在ParserService中,我添加了以下屬性:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ParserService : IParserService
現在,任何傳入ParserService的請求都由該類的單個原始實例處理。
如果有人知道沒有這種屬性的另一種獲得相同結果的方法,歡迎您分享。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.