应用SqlGeometry无法加载sqlserverspatial.dll

最近需要完成一个API,通过用户上传的经纬度判断用户的所在县市省,数据量相对不是很大所以把相关数据全部扔到了内存里知行,主要用到了SqlGeometry,

代码写完后运行本地没问题,扔到服务器上开始报异常“无法加载sqlserverspatial.dll”,一时蛋疼菊紧。难道还得装个sql server的服务吗。。。显然不用- -留下点笔记以备后用。。

Any application that uses Entity Framework’s spatial data type support to target SQL Server requires the ‘CLR Types for SQL Server’ to be available on the machine the application runs on. This also applies to applications that use SQL Server spatial data types directly, without using Entity Framework.(就是说想用SQL Server的空间模块功能一般得装个CLR Types for SQL Server)

When developing your application the CLR Types for SQL Server are usually installed system-wide, since they are included in Visual Studio. Issues arise when you try to deploy to a machine that does not have the CLR Types for SQL Server installed. Initially you will get the following InvalidOperationException.

Spatial types and functions are not available for this provider because the assembly 'Microsoft.SqlServer.Types' version 10 or higher could not be found.

If you were to find and deploy the Microsoft.SqlServer.Types assembly you’ll then get the following DllNotFoundException.

Unable to load DLL 'SqlServerSpatial110.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) ()

(正常思路是引用Microsoft.SqlServer.Types.dll就以为OK了。。然而依然会有异常

解决方案:

安装 Microsoft.SqlServer.Types NuGet package

PM> Install-Package Microsoft.SqlServer.Types
然后项目下出现:
看看readme就啥都解决了。。。
大致就是说想跑起来的话你可以提前加载SqlServerSpatial110.dll这样就不需要装System CLR Types for SQL Server了
然后这个分32位和64位的
然后如果是web可以在程序入口Globle,starup啥的加个 SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
如果是桌面应用啥的就在起始加个SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
 
最近部署时又蹦出来个Bug:
大致意思就是加载这个程序集“Microsoft.SqlServer.Types, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91“的时候没找到,

打开引用仔细看了看,明明哪里都是版本11.0.0.0

解决方式:

在配置文件中添加对指定程序集

 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
        <bindingRedirect oldVersion="10.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

 

posted @ 2016-01-26 09:18  liamyu  阅读(2086)  评论(0编辑  收藏  举报