2017-07-14 64 views
1

我有一個C#.NET Web應用程序使用EF連接到SQL Server數據庫,並且它引發'初始化字符串格式不符合規範開始索引0'錯誤。這隻發生在我的遠程服務器上,它在本地工作正常。很顯然,我的連接字符串是不正確的,我的問題不是直接關於這個錯誤。拋出一個錯誤,包括連接字符串

我的應用程序有5個匹配5個SQL Server數據庫的上下文,每個在我的web.config文件中都有一個連接字符串。我知道第一個上下文正在工作,但我不知道其他上下文中哪個是問題。 web.config中的5個連接字符串都非常相似,如果第一個(顯然工作正常)工作,我不明白爲什麼不工作。所有5個連接字符串用於工作,發生了什麼變化。

所以我想要更多的線索。

這裏是我的YSOD我的堆棧跟蹤:

[ArgumentException: Format of the initialization string does not conform to specification starting at index 0.] 
    System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) +1739 
    System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey) +191 
    System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules) +136 
    System.Data.SqlClient.SqlConnectionString..ctor(String connectionString) +76 
    System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) +35 
    System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) +241 
    System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) +78 
    System.Data.SqlClient.SqlConnection.set_ConnectionString(String value) +116 
    System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +931 
    System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext`1 interceptionContext) +509 
    System.Data.Entity.Internal.LazyInternalConnection.Initialize() +77 
    System.Data.Entity.Internal.LazyInternalConnection.get_Connection() +16 
    System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver) +508 
    System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) +370 
    System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +140 
    System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +72 
    System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +483 
    System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +177 
    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +274 
    System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +38 
    System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +77 
    System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21 
    System.Data.Entity.Internal.Linq.InternalSet`1.Find(Object[] keyValues) +32 
    Argus.RdAP.GetCodeFromIndex(Int32 IndexNumber) +140 
(etc...) 

OK,這裏是我的問題:這將是非常方便的,如果我能看到什麼不好的連接字符串了。我有一個很好的想法,即代碼中的問題在哪裏開始(即在Argus.RdAP.GetCodeFromIndex中)。是否有一個嘗試...趕上...拋出一段代碼,將包含實際連接字符串在拋出的異常?

對不起,如果這是一個愚蠢的問題。另外 - 歡迎訪問連接字符串或修復我的基礎問題的替代建議。

+0

我基本上會阻止將連接字符串的內容寫入異常(即日誌文件/服務器/ ...)連接字符串可能包括密碼等機密信息。 –

+0

我的連接字符串沒有密碼,這是一個非常臨時的措施,但很棒,謝謝。 – Shewan

回答

0

最終我找到了答案!

try 
{ 
    // here was the method I suspected of causing the problem 
} 
catch 
{ 
    throw new Exception(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString); 
}