I am using VS2008. I have a project, SystemSoftware project, connect with a database and we are using L2E. I have a RuntimeInfo class which contains some shared information there. It looks like this:
public class RuntimeInfo { public const int PWD_ExpireDays = 30; private static RuntimeInfo thisObj = new RuntimeInfo(); public static string AndeDBConnStr = ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString; private RuntimeInfo() { } /// <summary> /// /// </summary> /// <returns>Return this singleton object</returns> public static RuntimeInfo getRuntimeInfo() { return thisObj; } } Now I added a helper project, AndeDataViewer, to the solution which creates a simple UI to display data from the database for testing/verification purpose.
I don't want to create another set of Entity Data Model in the helper project. I just added all related files as a link in the new helper project.
In the AndeDataViewer project, I get the connection string from above RuntimeInfo class which is a class from my SystemSoftware project as a linked file. The code in AndeDataViewer is like this:
public class DbAccess : IDisposable { private String connStr = String.Empty; public DbAccess() { connStr = RuntimeInfo.AndeDBConnStr; } } My SystemSoftware works fine that means the RuntimeInfo class has no problem there. But when I run my AndeDataViewer, the statement inside above constructor,
connStr = RuntimeInfo.AndeDBConnStr; , throws an exception. The exception is copied here:
System.TypeInitializationException was unhandled Message="The type initializer for 'MyCompany.SystemSoftware.SystemInfo.RuntimeInfo' threw an exception." Source="AndeDataViewer" TypeName="MyCompany.SystemSoftware.SystemInfo.RuntimeInfo" StackTrace: at AndeDataViewer.DbAccess..ctor() in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\DbAccess.cs:line 17 at AndeDataViewer.Window1.rbRawData_Checked(Object sender, RoutedEventArgs e) in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\Window1.xaml.cs:line 69 at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) .... InnerException: System.NullReferenceException Message="Object reference not set to an instance of an object." Source="AndeDataViewer" StackTrace: at MyCompany.SystemSoftware.SystemInfo.RuntimeInfo..cctor() in C:\workspace\SystemSoftware\SystemSoftware\src\systeminfo\RuntimeInfo.cs:line 24 InnerException: I cannot understand this because it looks fine to me but why there is an exception? we cannot access static variable when a class is a linked class? A linked class should be the same as the local class I think. "Linked" here means when I add file I use "Add As Link".
how can I avoid this exception?
EDIT: I added the SystemSoftware's App.config to AndeDataViewer as a link. Then fixed that problem but I don't like it. How about if I have App.config in there already? I don't want to manually copy connectionstring there either because "manually" means no good.
for those have multiple projects share one database, this link may be helperful when you have trouble: MetadataException: Unable to load the specified metadata resource
ConfigurationManager.ConnectionStrings? I'm assumingDictionary<string, System.Data.SqlClient.SqlConnection>but then why would you need to fetch the ConnectionString from it?AndeDataViewerhaveSystemSoftwareas a dependency, then access its objects through theSystemSoftwarenamespace, although you'd also need to makeRuntimeInfopublic to do it.