.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号