我有一个静态连接工厂类,需要从配置中初始化连接参数
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;
}
}
}
从主要参数中我尝试初始化这些属性
MyConnectionFactory.Authority = "blahblah";
这会在静态类中引发空值异常。该值未到达班级。
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
设置静态类的静态属性值是否错误?我知道静态构造函数(),但它不带参数,我需要设置连接参数。静态工厂不是连接工厂的正确模式吗?如果我不使工厂静态化,我可以使整个事情正常进行,但这似乎不是正确的做法。
浮云间
相关分类