2

I have WCF service and use useful feature to lock execution of code among any threads and instances of WCF. I use static variable in such way:

    private static object _locker = new System.Object();
    public void MyMethod()
    {
        lock(_locker)
        {
            //some code
        }
    }

Because static variables are shared across all WCF instances(are global for the life of the application) the code in lock block always executes only once at the same time - for all clients and all their requests.

Now I need to port such logic to .NET CORE platform. But WCF is not implemented in .NET CORE.

How can I make service using .NET CORE (maybe ASP.NET CORE) that would be able to lock code sections in such way and store variables among all requests ?

Maksim
  • 59
  • 1
  • 7
  • You generally could rely on static fields being preserved in ASP.NET classic. The only scenarios where static fields got reset were when ASP.NET recycled the app pool (which wouldn't happen while a request was being processed) and when the page needed to be recompiled (which could be protected against by moving the static field to another class) – https://stackoverflow.com/a/8919336/1149773. I don't know whether ASP.NET Core has introduced any new nuances. – Douglas Mar 01 '19 at 10:57
  • Maybe [stackoverflow.com/questions/45527228](https://stackoverflow.com/questions/45527228/do-i-need-to-lock-singleton-in-asp-net-core) will help you – Martin Mar 01 '19 at 10:58

0 Answers0