2

Half a day of googling suggests, that it's a bit niche topic, and my question is quite specific. I'm using: VS2013, .NET 4.5, IIS 8.5

I have a ASP.NET website that needs to query a data source. Opening the data source is costly, but I can keep it open indefinitely.

My idea was: create a Command Line application or a Windows Service that will open the data source and then expose the querable objects to the ASP.NET website.

I don't like the idea of having this unmanaged (CommandLine) or managed apart from website (WinService) application that I have to deploy completely separately.

I've read that it is possible to create an always running WCF service hosted in IIS. I would like it to keep a list of object instances that would be returned as a result of a WCF call. Is that at all possible? If yes, how?

I've tried setting the WCF service AppPool to AlwaysRunning, enabling autostart on service application and I can access the service, but a simple test shows, that the service object is created every time anew:

public class MyService : IMyService{
{
private int _counter;
public int Test(){ return _counter++; }
}

My website creates a MyServiceClient from service reference and calls test - it returns 0 every time.

I've also found, that if I create any class in my WCF service application, I cannot access it from inside MyService methods. I can access though classes referenced from other projects. Why is that?

Gerino
  • 1,943
  • 1
  • 16
  • 21
  • Is out of the question but only as suggestion did you think in using web api ? – Leandro Bardelli Nov 10 '14 at 12:58
  • "Always running" generally means that the application pool/process does not get recycled/restarted it does not change the fact that each request runs in a separate thread. If you want a variable to persist for the life of the app domain then declare it as `static`. – Ben Robinson Nov 10 '14 at 13:03
  • @BenRobinson - oh my, I feel stupid now. You're completly right, it apparently fixed my problem (in test case). I'll apply it to the full solution and see if I run into any more trouble. – Gerino Nov 10 '14 at 13:14

1 Answers1

2

I think you're looking for a singleton service. By default the ServiceBehaviorAttribute.InstanceContextMode is set to PerSession. Instead set it to Single. Every client will then connect to the same instance of the service.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyService : IMyService{
{
    private int _counter;
    public int Test(){ return _counter++; }
}

Personally, I prefer the singleton approach over static as discussed in here

Community
  • 1
  • 1
Robert Graves
  • 2,290
  • 3
  • 20
  • 24
  • As far as I can tell, you're completly correct. Am I right thinking, that if I used standard settings of AppPool/IIS, it would simply recycle the pool (destroying the class) after a period of inactivity, but for most of the time it would work just as well? – Gerino Nov 10 '14 at 15:55
  • Yes I believe it should only instantiate on the first hit, and then stay in memory until the app pool is recycled after inactivity. – Robert Graves Nov 10 '14 at 16:23