0

I'm developing a service, which is to run 24/7 and is to be controlled over MVC web-site, but it's not that important. What does matter though, is that What I have now is (for windows service):

    protected override void OnStart(string[] args)
    {
        if (this.ServiceHost != null)
            this.ServiceHost.Close();

        this.ServiceHost = new ServiceHost(typeof(ParserService));
        this.ServiceHost.Open();
    }

And ParserService is as the following at the moment:

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();
    }
}

And the problem is: In Parser service, FacadeForParser is never seaved for the next call. Seems like I misunderstood lifecycle of WCF classes... So Can anyone tell me correct way to implement this part?

I do need: 1 instance of ParserFacade to work with it through all the requests ever handled by WCF part of the service.

Anatolyevich
  • 671
  • 5
  • 14

1 Answers1

0

I have found a solution for this case. It's not what I originally was thinking about, but result is exactly what I was looking for..

In ParserService I have added the following attribute:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ParserService : IParserService

And now any request coming to ParserService is handled by single original instance of the class..

If anybody knows another way of achieving the same result without such attribute, you are welcome to share.

Anatolyevich
  • 671
  • 5
  • 14
  • This is the way however, there is also concurrency to think about. That happens if an event is hit by many at the same time? Have a look here to decide on the desired behavior https://msdn.microsoft.com/en-us/library/ms731193(v=vs.110).aspx – Ares Jan 09 '16 at 17:20
  • I do know it's not concurrency friendly way to reach the goal. I's more of temporary resolution. Since that, I'm still at the point of searching of right way to do the task. So if someone could tell me the way to do things right, I'll be happy. Unfortunately, I don't see any straight-forward way on my own.. =( – Anatolyevich Jan 10 '16 at 18:34