首页 > 解决方案 > 如何在 WCF 服务应用程序中使用 Microsoft.SqlServer.Types

问题描述

我正在开发 WCF 服务应用程序。我想找到离某人最近的位置。我的问题是当我想调用与 System.Data.Entity.Spatial.DbGeography 相关的任何方法时,我收到此错误“空间类型和函数不适用于此提供程序,因为程序集 'Microsoft.SqlServer.Types' 版本 10 或更高版本可能找不到。”

我在 Visual Studio 2017 解决方案中通过 NuGet 安装 Microsoft.SqlServer.Types 并在调用 DbGeography 类之前调用​​加载 SqlServerTypes 程序集

    private bool _IsSqlServerTypesLoaded;
    private void CheckSqlServerTypes()
    {
        if (!_IsSqlServerTypesLoaded)
        {
           System.Data.Entity.SqlServer.SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.1016.290, Culture=neutral, PublicKeyToken=89845dcd8080cc91";
            SqlServerTypes.Utilities.LoadNativeAssemblies(System.IO.Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "bin"));
            _IsSqlServerTypesLoaded = true;
        }
    }

在尝试查询位置之前,我调用了 CheckSqlServerTypes(),但我仍然收到此错误“空间类型和函数不适用于此提供程序,因为找不到程序集 'Microsoft.SqlServer.Types' 版本 10 或更高版本。”

我的错误是什么?

标签: wcfdbgeography

解决方案


我发现了我的错误并想分享,也许对其他人有用

在上面的代码中,我将“版本=14.0.1016.290”更改为“版本=14.0.0.0”

也将其添加到 web.config

<assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="Microsoft.SqlServer.Types" publicKeyToken="89845dcd8080cc91" culture="neutral" />
      <bindingRedirect oldVersion="10.0.0.0-11.0.0.0" newVersion="14.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>

一切正常


推荐阅读