-1

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
Tauqir
  • 155
  • 2
  • 10

1 Answers1

1

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.

Sohaib Jundi
  • 1,576
  • 2
  • 7
  • 15
  • When you try to set it, the constructor have to be called before, since it is static. `ClientId` at this point is still null, so `new ClientCredential(ClientId, ClientSecret);` would throw an exception – Sohaib Jundi Aug 10 '19 at 17:48
  • Sorry, but the exception is not in ```new AuthenticationContext(Authority);``` It is in the property setter. I cannot understand what is wrong with setting a static class's static properties anytime I want. – Tauqir Aug 10 '19 at 17:58
  • I edited the answer, please check it as it is hopefully clearer – Sohaib Jundi Aug 10 '19 at 18:04
  • Understood. So the null value exception that points to the set method of Authority property is actually due to the static ClientCredential call. Thanks for your insight. I would never have guessed it. I had moved it there as an optimization. It was in createConnection() initially. Moving it back there solved the problem. Many thanks. – Tauqir Aug 10 '19 at 18:34