Search code examples
c#initializationstatic-classes

Initializing properties of static class fails with null value exception


I have a static connection factory class and need to initialize the connection parameters from configuration

namespace MyApp.Common.LinqToDB
{
    public static class MyConnectionFactory
    {
        public static string Authority { get; set; }
        public static string Target { get; set; }
        public static string ConnectionString { get; set; }
        public static string ClientId { get; set; }
        public static string ClientSecret { get; set; }
        private static ClientCredential ClientCredential = new ClientCredential(ClientId, ClientSecret);

    public static IDbConnection createConnection()
        {
            AuthenticationContext authenticationContext = new AuthenticationContext(Authority);
            AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(Target, ClientCredential).Result;
            SqlConnection MyDataConnection = new SqlConnection(ConnectionString);
            MyDataConnection.AccessToken = authenticationResult.AccessToken;

            return MyDataConnection;
        }
    }
}

From the main parogram I try to initialize these properties

MyConnectionFactory.Authority = "blahblah";

this throws a null value exception inside the static class. The value does not get to the class.

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'Workspace.Common.LinqToDB.WorkspaceConnectionFactory' threw an exception.
  Source=Common
  StackTrace:
   at Workspace.Common.LinqToDB.WorkspaceConnectionFactory.set_Authority(String value) in 

........
Inner Exception 1:
ArgumentNullException: Value cannot be null.
Parameter name: clientId

Is it wrong to set the values of static properties of a static class. I know about static constructor() but it does not take a parameters and I need to set the connection parameters. Is static factory not the right pattern for a connection factory. I can make this whole thing work if I don't make the factory static, but that does not seem the right thing to do.


Solution

  • private static ClientCredential ClientCredential = new ClientCredential(ClientId, ClientSecret); would be compiled as:

    private static ClientCredential ClientCredential;
    
    //Static constructor
    static MyConnectionFactory()
    {
      ClientCredential = new ClientCredential(ClientId, ClientSecret);
    }
    

    A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
    https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors

    Now, when you try to assign Authority, the static constructor have to be called before. At this point, ClientId and ClientSecret are still null, which is causing the exception.