我有一个包含由一个asp.net web应用程序和一个WCF Web服务共享类类库。 图书馆内的类需要从数据库中检索信息,所以图书馆在App.config的连接字符串中的条目,并检索连接字符串的方法。
以下是我声明我的连接字符串中的app.config:
<configuration>
<connectionStrings>
<add name="connstring"
connectionString="Data Source=MYSERVER;Initial Catalog=MY_DB;User Id=mylogin;Password=mypassword;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
[...]
</configuration>
而这里的检索连接字符串的方法:
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Module modGen
Public Function gGetConnectionString() As String
Dim conn As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("connstring")
Dim connString As String = conn.ConnectionString
Return connString
End Function
问题是,该方法运行时,“conn将”得到“没有”的值,然后下面的异常被抛出:
System.TypeInitializationException was caught
Message=The type initializer for 'CHK.TRK.CLS.modGen' threw an exception.
Source=CHK.TRK.CLS
TypeName=CHK.TRK.CLS.modGen
StackTrace:
at CHK.TRK.CLS.modGen.gGetConnectionString()
at CHK.TRK.CLS.modAuth.CheckUser(String logon, String pwHash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modAuth.vb:line 141
at CHK.TRK.CLS.modAuth.Authenticate(String logon, String pwhash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modAuth.vb:line 9
at CHKTRKWebService.CheckTrackService.Authenticate(String login, String pwHash) in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKWebService\CheckTrackService.svc.vb:line 20
InnerException: System.NullReferenceException
Message=Object reference not set to an instance of an object.
Source=CHK.TRK.CLS
StackTrace:
at CHK.TRK.CLS.modGen..cctor() in H:\WorkSpace\CHKTRK\CHKTRK\CHKTRKClasses\Mod\modGen.vb:line 138
InnerException:
是不是有什么毛病我宣布在app.config文件我的连接字符串,或检索它的方法呢?