注意:這是 Microsoft.Data.SqlClient 的問題,它是 SqlConnection 的實作。如果我創建一個實體 (_sqlConnection = new SqlConnection();) 然后檢查狀態 (_sqlConnection.State) 我得到一個 NullReferenceException。如果我分配了一個有效的連接字串,仍然是 Null 例外。
解決方案(以節省您的時間)是恢復使用 System.Data.SqlClient,一切都很好。我將其余部分留在這里,以防它有助于追查原因 - 或者如果其他人遇到類似問題。
我有一個 WPF .net 4.7.2 應用程式,它在我從 VS 構建和運行時運行(或在除錯或發布中雙擊 .exe。)如果我生成一個 .msi 檔案并從那里安裝,我會收到一個空例外錯誤將日志寫入 MS SQL 服務器(但不寫入本地檔案)時
我在用:
- 串行日志:2.10.0
- Serilog.Sinks.Console:4.0.0
- Serilog.Sinks.File: 5.0.0
- Serilog.Sinks.PeriodicBatching:2.3.0
- Serilog.Sinks.MSSqlServer:5.6.1
- MS.Data.SqlClient: 3.0.1
- System.Data.Common: 4.3.0
- System.Data.SqlClient:4.8.3
.msi 檔案是使用以下方法創建的:Microsoft Visual Studio 安裝程式專案(可從https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects&ssr=false#overview 獲得)
以下代碼已被修改以消除所有干擾以及任何專有資訊。請記住,除了作為 .msi 檔案安裝時,這都可以正常作業。
當我的應用程式啟動時,我呼叫以下內容: Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
導致問題的呼叫示例:LogDebug("This is a developer test of LogDebug by {user}", );
代碼:
// Primary method
public void LogDebug(string msgtemplate, params object[] parms){
var LogCfg = GetStdLogConfig();
Log.Logger = LogCfg.CreateLogger();
Log.Warning(msgtemplate, parms);
Log.CloseAndFlush(); // error happens on this line
}
protected virtual LoggerConfiguration GetStdLogConfig()
{
LoggerConfiguration logcfg = null;
logcfg = new LoggerConfiguration()
.MinimumLevel.ControlledBy(StandardLevelSwitch)
.Enrich.WithProperty("Fid1", AppValues.VersionNo)
.Enrich.WithProperty("Fid2", AppValues.UserId)
.Enrich.WithProperty("Fid3", getUserRightsString())
.Enrich.WithProperty("Fid4", AppValues.AppCompileMode);
AddLocalFile(logcfg, "std"); // this writes to local file just fine.
AddSql(logcfg, "std");
return logcfg;
}
protected virtual LoggerConfiguration AddSql(LoggerConfiguration logcfg, string mode = "")
{
try
{
var logTable = "App_Logs"; // Renamed for sample
var sinkOptions = new MSSqlServerSinkOptions { TableName = logTable };
var columnOptionVals = new ColumnOptions
{
AdditionalColumns = new System.Collections.ObjectModel.Collection<SqlColumn>()
};
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid1", System.Data.SqlDbType.NVarChar, true, 50));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid2", System.Data.SqlDbType.NVarChar, true, 15));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid3", System.Data.SqlDbType.NVarChar, true, 100));
columnOptionVals.AdditionalColumns.Add(new SqlColumn("Fid4", System.Data.SqlDbType.NVarChar, true, 20));
columnOptionVals.Store.Remove(StandardColumn.MessageTemplate);
columnOptionVals.LogEvent.DataLength = 2048;
columnOptionVals.PrimaryKey = columnOptionVals.TimeStamp;
columnOptionVals.TimeStamp.NonClusteredIndex = true;
// <Valid ConnectionString> is the same as used to do other updates without issue
logcfg.WriteTo.MSSqlServer(
connectionString: <Valid ConnectionString>,
sinkOptions: sinkOptions,
columnOptions: columnOptionVals
);
return logcfg;
}
錯誤發生在 Log.CloseAndFlush(): 中。
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
If I continue, this error shows:
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
This exception was originally thrown at this call stack:
Microsoft.Data.SqlClient.SqlConnection.OpenAsync(System.Threading.CancellationToken)
System.Data.Common.DbConnection.OpenAsync()
Serilog.Sinks.MSSqlServer.Platform.SqlClient.SqlConnectionWrapper.OpenAsync()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in exceptionservicescommon.cs
If I view this exception, in the StackTrace, I notice additional info:
at Microsoft.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken) at System.Data.Common.DbConnection.OpenAsync() at Serilog.Sinks.MSSqlServer.Platform.SqlClient.SqlConnectionWrapper.d__6.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in f:\dd\ndp\clr\src\BCL\system\runtime\exceptionservices\exceptionservicescommon.cs:line 133
It then continues (but nothing is logged to the SQL server)
In the output window I see (due to SelfLog call above):
Exception thrown: 'System.NullReferenceException' in mscorlib.dll
Object reference not set to an instance of an object.
除錯.exe時,SqlConnection實體化后,很多屬性都是NullExceptions,不是NULL。試圖給它們賦值什么也不做,它們仍然是 NullExceptions。但它只發生在這個呼叫中,SqlConnection 的所有其他實體都按預期作業。
解決方案中的所有其他專案都使用 System.Data.SqlClient - 這是所有代碼作業的內容,只有這個接收器沒有。
uj5u.com熱心網友回復:
我根據電子郵件中的開放資訊編輯了圖書館。最終修復它(有或沒有這些步驟的幫助)是用默認的系統 sql 庫替換 Github MS SQL 庫。
//using Microsoft.Data.SqlClient;
using System.Data.SqlClient;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/330495.html
標籤:C# 小白 空引用异常 串行 system.data.sqlclient
