C# ASP.NET MVC Windows Service NULL out Static Instance Properties in Data Layer?
Here is my scenario.
I have an ASP.NET MVC solution with 3 projects. Data, Web, and Windows Service. The Data layer has static (non thread safe) properties which goes and gets data once otherwise stores in static private field. See below:
private static int? _userCount;
public stati开发者_StackOverflow社区c int UserCount
{
get
{
if (!_userCount.HasValue)
{
_userCount = GetUserCount();
}
return _userCount.Value;
}
set
{
_userCount = value;
}
}
This works great for my needs. In the Web layer I call into a Service method in my data layer whenever adding a new user and set UserCount = NULL
Thus the next time there is a hit to the site it will go fetch a fresh count. This works as expected too.
I also have a Windows Service which can create Users via an import process. It calls into the same SaveUser
method as the web layer which again should be Nulling out the UserCount property. I would then think I should be able to be on the website and refresh the page and have it again get a new user count. However this is not the case.
I can step through the code and see the Service hit the UserCount = NULL
but its like the web layer doesn't recognize it even though it is calling into the same property.
I have to do an IIS Reset anytime the Windows Service brings in a User in order to clear out the static properties. Any thoughts on why this would behave differently with creating the user in the Service vs. on the Web layer via the UI?
Let me see if I have this straight. You have a library that handles save user, which nulls out a count value. It works in MVC, but not in your windows service, because it is not being seen as null in your MVC application.
If I have that correct, let me be more explicit. You have a library to handle users being used by your MVC application. The same library is also being used by a windows service.
If I have that correct, did you get the reason things are not working. If you are stuck on static, start thinking process boundaries and ask "Is the MVC application using the same process as the windows service"?
Short answer: If this functionality is in a library, used by two separate applications, it is behaving correctly, as each application stores its own shared and instance bits in its own process space. It is nulling out, but only for the service.
Cure? Have the windows service call a method in the MVC application to indicate it has changed things is certainly one option. Another is to use a different method of invalidating user count when values have changed.
I think the problem is that you're operation in two different application spaces. The Web and Service layers each have their own application space in which they each have their own static properties. The data layer is just an assembly that's loaded into each of the application spaces. It's only if you persist this data to disk, or an in-memory database, that you'll see the same data in each application space.
精彩评论