使用SqlGeography.STGeomFromText创建Polygon时抛出加载库异常
问题
使用最新版本160的Microsoft.SqlServer.Types库时,创建Polygon对象会抛出异常,但创建SqlGeography.Point对象可正常运行。
相关代码
string polyText = "POLYGON (( 45.3972179 8.8406638000000157, 45.4215054 8.9151509000000715, 45.4001544 8.9361126999999669, 45.4016369 8.9607184999999845, 45.3851877 8.9621395000000348, 45.3853039 8.948319999999967, 45.3747296 8.9535720999999739, 45.3858019 8.9360727000000679, 45.3579525 8.8890794000000142, 45.3414371 8.90664240000001, 45.3452409 8.91328980000003, 45.3273192 8.92622259999996, 45.3185438 8.9208002999999962, 45.3405949 8.8954118000000335, 45.3471543 8.866777299999967, 45.3587372 8.8690788999999768, 45.3675493 8.8504374999999982, 45.3972179 8.8406638000000157, 45.3972179 8.8406638000000157, 45.3972179 8.8406638000000157 ))"; SqlGeography poly = SqlGeography.STGeomFromText(new SqlChars(polyText), 4326);
异常堆栈信息
SQL Server Spatial库无法加载。错误代码: 126 (HResult: 0x80131524). Microsoft.SqlServer.Types System.IO.FileNotFoundException: SQL Server Spatial库无法加载。错误代码: 126 (HResult: 0x80131524). 在 Microsoft.SqlServer.Types.GLNativeMethods.ThowIfGLNativeLibraryNotLoaded() 在 Microsoft.SqlServer.Types.GLNativeMethods.GeodeticIsValid(GeoData& g, Double eccentricity, Boolean forceKatmai) 在 Microsoft.SqlServer.Types.SqlGeography.IsValidExpensive(Boolean forceKatmai) 在 Microsoft.SqlServer.Types.SqlGeography..ctor(GeoData g, Int32 srid) 在 Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid) 在 Microsoft.SqlServer.Types.SqlGeography.STGeomFromText(SqlChars geometryTaggedText, Int32 srid) 在 webNet.Area.Default.DrawShapesFromSql(String polyText, String areaName) 位置 C:\Project\webNet\Area\Default.aspx.cs:行号 474 在 webNet.Area.Default.LoadForm() 位置 C:\Project\webNet\Area\Default.aspx.cs:行号 93
解决建议
- 检查原生依赖库:Microsoft.SqlServer.Types 160版本依赖
SqlServerSpatial160.dll原生库,确认项目输出目录或系统PATH路径中存在对应CPU架构(x86/x64)的该文件。若缺失,可从NuGet包的runtimes/win-[x86/x64]/native目录复制到项目输出目录。 - 安装SQL Server共享组件:开发环境可安装SQL Server 2022的共享功能组件,自动部署所需的空间原生库。
- 修复WKT格式:当前多边形WKT重复了三次起始点,标准格式仅需首尾各一次即可闭合,删除多余重复点后再测试,排除格式问题引发的间接错误。
- 配置程序集绑定:在
web.config或app.config中添加绑定重定向,确保运行时加载正确版本的组件:<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-16.0.0.0" newVersion="16.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime>
内容的提问来源于stack exchange,提问作者Kamran Musayev
相关产品推荐
相关产品推荐

