当使用地理空间操作时,在生产中出现BadImageFormatException

本文关键字:生产中 BadImageFormatException 操作 空间 | 更新日期: 2023-09-27 18:02:51

我正在做一个windows服务项目,它使用EF的地理空间特性和SQLServer。我在生产中使用SQL Server 2014。我已经在服务器上安装了SQLSysClrTypes.msi至少2-3次,并且NuGet正在管理我的Microsoft.SqlServer.Types包。我想这不会是x86/x64的问题,因为我有一个SqlServerTypes文件夹,其中,x86以及x64 dll驻留在他们单独的文件夹(即x86和x64)。

我面临的详细例外情况如下:

Inner Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
   at Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIntersects(GeoMarshalData g1, GeoMarshalData g2, Double eccentricity, Boolean& result)
   at Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIntersects(GeoData g1, GeoData g2, Double eccentricity)
   at Microsoft.SqlServer.Types.SqlGeography.STIntersects(SqlGeography other)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Data.Entity.SqlServer.SqlSpatialServices.Intersects(DbGeography geographyValue, DbGeography otherGeography)
   at System.Data.Entity.Spatial.DbGeography.Intersects(DbGeography other)
谁能告诉我为什么我可能会面临这个例外?

以下是微软提供的解决这个错误的最好方法:https://msdn.microsoft.com/en-us/library/k7137bfe.aspx

当使用地理空间操作时,在生产中出现BadImageFormatException

当您试图在x64进程中加载x86 DLL时,您会得到该异常,反之亦然。有32位和64位的Microsoft.SqlServer.Types DLL,您需要正确的DLL。不要记住这最后一项,但您必须进行测试,但有些本机dll甚至不能与AnyCPU一起工作,您必须特别匹配。