2010-04-30 125 views
0

我正在使用VS2008。我有一個項目,SystemSoftware項目,連接數據庫,我們使用L2E。我有一個RuntimeInfo類,其中包含一些共享信息。它看起來像這樣:C#,無法理解這個錯誤?

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; 
     } 
} 

現在我增加了一個輔助項目,AndeDataViewer,爲其創建一個簡單的用戶界面從數據庫中進行測試/驗證目的顯示數據的解決方案。我不想在助手項目中創建另一組Entity Data Model。我只是將所有相關文件添加爲新助手項目中的鏈接。

AndeDataViewer項目,我從上面RuntimeInfo類,這是從我SystemSoftware項目作爲鏈接文件類獲得connection string。在AndeDataViewer的代碼是這樣的:

public class DbAccess : IDisposable 
    { 
     private String connStr = String.Empty; 
     public DbAccess() 
     {   
      connStr = RuntimeInfo.AndeDBConnStr; 
     } 
} 

SystemSoftware工作正常,這意味着該RuntimeInfo類沒有問題存在。但是,當我跑我的AndeDataViewer,聲明內上述構造,

connStr = RuntimeInfo.AndeDBConnStr; 

,拋出異常。例外是複製在這裏:

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: 

我不明白,因爲它看起來很好,但爲什麼有一個例外?當一個類是一個鏈接類時,我們不能訪問靜態變量?鏈接的類應與我認爲的本地類相同。這裏的「鏈接」意味着當我添加文件時,我使用「添加爲鏈接」。

我該如何避免此異常?

EDIT: 

我將SystemSoftware的App.config作爲鏈接添加到AndeDataViewer中。然後解決這個問題,但我不喜歡它。如果我已經有App.config,那該怎麼辦?我不想在那裏手動複製連接字符串,因爲「手動」意味着沒有好處。

對於那些有多個項目共享一個數據庫,這個環節可能是helperful當你有麻煩:MetadataException: Unable to load the specified metadata resource

+0

什麼數據類型是'ConfigurationManager.ConnectionStrings'?我假設'Dictionary ',但爲什麼你需要從中獲取ConnectionString? – Powerlord 2010-04-30 17:51:24

+0

至於依賴關係,你可以讓'AndeDataViewer'具有'SystemSoftware'作爲依賴關係,然後通過'SystemSoftware'命名空間訪問它的對象,儘管你還需要公開'RuntimeInfo'來執行它。 – Powerlord 2010-04-30 17:54:49

回答

3

如果你會看到這個應用程序config文件沒有定義的連接字符串名稱「AndeDBEntities」。

public static string AndeDBConnStr = 
     ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString; 

是訪問由ConnectionStrings["AndeDBEntities"]返回的對象,其可能不存在的ConnectionString屬性。

+0

這是否意味着我的AndeDataViewer中需要引用SystemSoftware項目的App.config文件? – 5YrsLaterDBA 2010-04-30 17:20:08

+0

項目中的引用在運行時並不具有任何相關性。當您的項目中有一個App.config時,Visual Studio將其複製到build文件夾並將其重命名爲AssemblyName.ext.config。因此,它不一定要在項目中,但它必須存在於您運行應用程序的任何地方。 – 2010-04-30 17:25:21

0

你有你的靜態構造函數中的異常。明確地創建它並在那裏移動所有靜態初始化器。然後調試:)

0

AndeDataViewer項目沒有配置條目。假設這是一個Winforms應用程序,您需要在該項目中定義一個app.config。