locked
Static instance of Microsoft.Extensions.Configuration.IConfiguration thread safety RRS feed

  • Question

  • User-1350516731 posted

    Hello all!

    Does the 

    private static IConfiguration Config;

    guarantee thread safe access via several controllers instances? Do I need to use lock?

    Thank you.

    Thursday, May 28, 2020 10:23 AM

All replies

  • User1120430333 posted

    Any thing that is static is a 'static cling' situation and is not considered loosely coupled and clean coding. IMO, you should be using an IoC  such as the Core Services IoC and dependency injection.

     https://docs.microsoft.com/en-us/archive/msdn-magazine/2016/may/asp-net-writing-clean-code-in-asp-net-core-with-dependency-injection

    Thursday, May 28, 2020 12:23 PM
  • User475983607 posted

    Alex9

    Hello all!

    Does the 

    private static IConfiguration Config;

    guarantee thread safe access via several controllers instances? Do I need to use lock?

    Thank you.

    Not enough information provided.  Generally configuration is read only.  If  there are no updates then there's no chance of dirty data.   It does beg the question, why did you decide on this programming pattern when the standard pattern found in every beginning level tutorial is a private readonly?  What are you trying to do?

    Thursday, May 28, 2020 1:28 PM
  • User-474980206 posted

    If it’s read only, and initialed in startup, and the implementation is threadsafe, then it should be thread safe. 

    Thursday, May 28, 2020 2:29 PM