.net framework 4.7 c# 鏈接Npgsql 的問題 解決 “Npgsql.TypeMapping.GlobalTypeMapper”的類型初始值設定項引發異常
環境:.net framework 4.7.2
nuget: Npgsql 4.1.14
項目: webapi
引入Npgsql程序包后,對數據庫進行查詢報錯,如下:
DataSet dataSet = new DataSet(); string sql = "SELECT * FROM xxx"; NpgsqlDataAdapter ad = new NpgsqlDataAdapter(sql, SqlConn); ad.Fill(dataSet); dataGridView1.DataSource = dataSet.Tables[0];
System.TypeInitializationException: “Npgsql.TypeMapping.GlobalTypeMapper”的類型初始值設定項引發異常。 ---> System.Reflection.ReflectionTypeLoadException: 無法加載一個或多個請求的類型。有關更多信息,請檢索 LoaderExceptions 屬性。 在 System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) 在 System.Reflection.Assembly.GetTypes() 在 Npgsql.TypeMapping.GlobalTypeMapper.SetupGlobalTypeMapper() 在 Npgsql.TypeMapping.GlobalTypeMapper..cctor() --- 內部異常堆棧跟蹤的結尾 --- 在 Npgsql.TypeMapping.ConnectorTypeMapper..ctor(NpgsqlConnector connector) 在 Npgsql.NpgsqlConnector.<LoadDatabaseInfo>d__147.MoveNext() --- End of stack trace from previous location where exception was thrown --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Npgsql.NpgsqlConnector.<Open>d__146.MoveNext() --- End of stack trace from previous location where exception was thrown --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 Npgsql.ConnectorPool.<AllocateLong>d__28.MoveNext() --- End of stack trace from previous location where exception was thrown --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Npgsql.NpgsqlConnection.Open() 在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
解決辦法:
對比了config文件中的程序集引用,發現現場項目缺少以下配置:
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Text.Encodings.Web" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
程序員何苦為難程序員!

浙公網安備 33010602011771號