C# 无法加载 SqlServerSpatial.dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14265845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:15:39  来源:igfitidea点击:

Unable to load SqlServerSpatial.dll

c#.netsql-serverclrgeospatial

提问by VBAHole

I am trying to use the SqlServer Spatial CLR types in a C# .Net project. I want to use SqlGeometry to query spatial records out of my db.

我正在尝试在 C# .Net 项目中使用 SqlServer Spatial CLR 类型。我想使用 SqlGeometry 从我的数据库中查询空间记录。

I have this working on my local machine in a unit test running in Visual Studio 2010 hitting a remote SqlServer machine. All good.

在 Visual Studio 2010 中运行的单元测试中,我在我的本地计算机上运行了此操作,并访问了远程 SqlServer 计算机。都好。

I then publish a WCF Rest service to my local IIS instance that has a service that hits the same class library as the unit test to do some spatial querying and it fails.

然后,我将 WCF Rest 服务发布到我的本地 IIS 实例,该实例的服务与单元测试访问相同的类库以执行一些空间查询,但它失败了。

I get an error saying

我收到一条错误消息

Unable to load DLL SqlServerSpatial.dll : The specified module could not be found.

无法加载 DLL SqlServerSpatial.dll:找不到指定的模块。

I have googled this and found many, many answers - none work for me. I have:

我用谷歌搜索了这个并找到了很多很多答案 - 没有一个对我有用。我有:

  • registered the CLR types with the GAC
  • install the 64-bit, and later also, the 32-bit version of the VC++
  • tried many variations of using different Microsoft.SqlServer.Types dll versions
  • 向 GAC 注册了 CLR 类型
  • 安装 64 位,然后安装 32 位版本的 VC++
  • 尝试了许多使用不同 Microsoft.SqlServer.Types dll 版本的变体

The only thing I have not done, and frankly refuse to do, is to install anything on the actual SqlServer box. This seems unnecessary to me.

我唯一没有做过并且坦率拒绝做的事情是在实际的 SqlServer 机器上安装任何东西。这对我来说似乎没有必要。

At this point the only thing that I can think is causing this is a permissions issue because it is running in an IIS app pool and not inside Studio where it works in the unit test.

在这一点上,我能想到的唯一原因是权限问题,因为它在 IIS 应用程序池中运行,而不是在它在单元测试中工作的 Studio 内运行。

Note that in my project I NEVER make reference to the dll mentioned in the error message. That dll is present on the sql box but I can't add it to studio as it gives some message when i try to. I'm running out of things to try here. It's 90's dll hell all over again.

请注意,在我的项目中,我从不引用错误消息中提到的 dll。该 dll 存在于 sql 框中,但我无法将其添加到工作室,因为它会在我尝试时提供一些消息。我在这里尝试的东西快用完了。又是 90 年代的 dll 地狱。

回答by Marcelo Mason

I had the same problem on a Windows Server 2012 machine. It had an SqlServerSpatial110.dll file in \Windows\System32, but no SqlServerSpatial.dll. The solution was installing the Microsoft System CLR Types for SQL Server 2008 R2 on the machine.

我在 Windows Server 2012 机器上遇到了同样的问题。它在 \Windows\System32 中有一个 SqlServerSpatial110.dll 文件,但没有 SqlServerSpatial.dll。解决方案是在机器上安装 Microsoft System CLR Types for SQL Server 2008 R2。

  1. http://www.microsoft.com/en-us/download/details.aspx?id=26728
  2. Click Download
  3. Check off one of thesedepending on your processor architecture:

    • 1033\x64\SQLSysClrTypes.msi
    • 1033\x86\SQLSysClrTypes.msi
    • 1033\IA64\SQLSysClrTypes.msi
  4. Click Next

  1. http://www.microsoft.com/en-us/download/details.aspx?id=26728
  2. 点击下载
  3. 根据您的处理器架构检查其中之一

    • 1033\ x64\SQLSysClrTypes.msi
    • 1033\ x86\SQLSysClrTypes.msi
    • 1033\ IA64\SQLSysClrTypes.msi
  4. 单击下一步

回答by Jason

I was having issues on a Windows Server 2008 R2 machine (Azure VM), but none of the steps above were able to fix the issue. I installed the CLR types. I put the files in my web application's BIN folder. Still nothing. I finally came across this blog by the folks at Microsoft and it worked. I'm leaving the url here in case it can help anyone else.

我在 Windows Server 2008 R2 机器(Azure VM)上遇到问题,但上述步骤均无法解决问题。我安装了 CLR 类型。我将文件放在我的 Web 应用程序的 BIN 文件夹中。依然没有。我终于在微软的人那里看到了这个博客,它奏效了。我把网址留在这里,以防它可以帮助其他人。

http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx

http://blogs.msdn.com/b/adonet/archive/2013/12/09/microsoft-sqlserver-types-nuget-package-spatial-on-azure.aspx

Since the above link no longer works (thanks MSFT!), I've put instructions below:

由于上面的链接不再有效(感谢 MSFT!),我在下面提供了说明:

  1. Open Visual Studio and open the NuGet Package Manager
  2. Search for "Microsoft.SqlServer.Types"
  3. Install...
  1. 打开 Visual Studio 并打开 NuGet 包管理器
  2. 搜索“Microsoft.SqlServer.Types”
  3. 安装...

This package will install the necessary .DLLs into your solution/project. It will also copy some additional libraries directly into your /bin directory. You must wire up references to these additional libraries in your global.asax.cs/vb file. There are instructions on how to do this included in the NuGet package. Below is a direct link to the NuGet Package (hopefully MSFT doesn't move this into oblivion too).

该软件包将把必要的 .DLL 安装到您的解决方案/项目中。它还会将一些额外的库直接复制到您的 /bin 目录中。您必须在 global.asax.cs/vb 文件中连接对这些附加库的引用。NuGet 包中包含有关如何执行此操作的说明。下面是 NuGet 包的直接链接(希望 MSFT 也不会将其遗忘)。

https://www.nuget.org/packages/Microsoft.SqlServer.Types/

https://www.nuget.org/packages/Microsoft.SqlServer.Types/

回答by davioooh

My problem was similar to yours: I installed my ASP.NET MVC project on a remote Azure Virtual Machine and I got this exception:

我的问题与您的类似:我在远程 Azure 虚拟机上安装了 ASP.NET MVC 项目,但出现此异常:

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

To solve the issue I followed these steps:

为了解决这个问题,我按照以下步骤操作:

  1. I added the reference to the missing package in my project:

    PM> Install-Package Microsoft.SqlServer.Types
    
  2. Then I forced the "Copy to output directory" option to "Copy always" for the SqlServerSpatial110.dll(probably this step is not strictly required...)

  3. For ASP.NET projects, you need to add the following line of code to the Application_Startmethod in Global.asax.cs:

    SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
    

    This last step was fundamentalfor me, because whitout this line of code the DLL is not loaded by the web application.

  1. 我在我的项目中添加了对缺失包的引用:

    PM> Install-Package Microsoft.SqlServer.Types
    
  2. 然后我将“复制到输出目录”选项强制为“始终复制” SqlServerSpatial110.dll可能这一步不是严格要求的......

  3. 对于 ASP.NET 项目,您需要在 中的Application_Start方法中添加以下代码行Global.asax.cs

    SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
    

    最后一个步骤是根本对我来说,因为whitout这行代码的DLL不是由Web应用程序加载。

回答by Hossein Narimani Rad

I have been using Microsoft.SqlServer.Types.dllin WPF and ASP.NET apps to work with SqlGeometrytype and spatial queries for years (since v.10) and here is the latest tips I found to successfully load the SqlServerSpatialXXX.dllas one of the prerequisites of the Microsoft.SqlServer.Types.dll.

我一直在使用Microsoft.SqlServer.Types.dll的WPF和ASP.NET应用程式与工作SqlGeometry类型和空间查询多年(从第10节),这里是我发现成功加载最新提示SqlServerSpatialXXX.dll为的先决条件之一Microsoft.SqlServer.Types.dll

  • SqlGeometryand SqlGeographytypes can be used in VS projects (e.g. C#) by referencing the Microsoft.SqlServer.Types.dll.
  • Microsoft.SqlServer.Types.dllis a managed library and has some unmanaged library as prerequisites and they are like SqlServerSpatialXXX.dlland msvcrXXX.dll
  • Since Sql Server 2008, different versions of Microsoft.SqlServer.Types.dllare available, however, I don't see any functionality change from 2012 on.
  • SqlGeometrySqlGeography类型可以通过引用Microsoft.SqlServer.Types.dll.
  • Microsoft.SqlServer.Types.dll是一个托管库,并且有一些非托管库作为先决条件,它们就像SqlServerSpatialXXX.dllmsvcrXXX.dll
  • 从 Sql Server 2008 开始,可以使用不同的版本Microsoft.SqlServer.Types.dll,但是,从 2012 年开始,我没有看到任何功能更改。

Consider 64bit/32bit issues

考虑 64 位/32 位问题

  • For 64 bit machanies, if you install CLR Types for Sql Server, you can find 64bit versions of these prerequisites files in Windows/System32and also you can find 32bit versions of prerequisites files in Windows/SysWOW64folder
  • If CLR Types are not installed on a machine, You should manually load proper versions (32bit/64bit) of these prerequisites based on your project (32bit or 64bit) otherwise you will errors like
  • 对于 64 位机器,如果您安装了 CLR Types for Sql Server,您可以在Windows/System32 中找到这些先决条件文件的 64 位版本,并且您还可以在Windows/SysWOW64文件夹中找到 32 位版本的先决条件文件
  • 如果机器上没有安装 CLR 类型,您应该根据您的项目(32 位或 64 位)手动加载这些先决条件的正确版本(32 位/64 位),否则您会出现类似错误

Error Loading SqlServerSpatialXXX.dll

加载 SqlServerSpatialXXX.dll 时出错

You can check 32bit/64bit issue at runtime in C# using Environment.Is64BitProcess. Here is a sample code:

您可以在 C# 中使用Environment.Is64BitProcess. 这是一个示例代码:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr LoadLibrary(string libname);

private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
{
    var path = Path.Combine(nativeBinaryPath, assemblyName);

    if (!File.Exists(path))
    {
        throw new FileNotFoundException($"{path} not found");
    }

    var ptr = LoadLibrary(path);
    if (ptr == IntPtr.Zero)
    {
        throw new Exception(string.Format(
            "Error loading {0} (ErrorCode: {1})",
            assemblyName,
            Marshal.GetLastWin32Error()));
    }          
}

public static void LoadNativeAssembliesv13(string rootApplicationPath)
{
    var nativeBinaryPath = Environment.Is64BitProcess
    ? Path.Combine(rootApplicationPath, @"SqlServerTypes\x64\")
    : Path.Combine(rootApplicationPath, @"SqlServerTypes\x86\");

    LoadNativeAssembly(nativeBinaryPath, "msvcr120.dll");
    LoadNativeAssembly(nativeBinaryPath, "SqlServerSpatial130.dll");
}

Consider binary path in different project typesIt is recommended to have a folder named SqlServerTypesin the execution path of your project like this

考虑不同项目类型下的二进制路径建议你的项目执行路径下有一个名为SqlServerTypes的文件夹像这样

SqlServerTypes>x64

SqlServerTypes>x64

SqlServerTypes>x32

SqlServerTypes>x32

and load unmanaged assemblies like this

并像这样加载非托管程序集

Utilities.LoadNativeAssembliesv13(Environment.CurrentDirectory); //WPF
Utilities.LoadNativeAssembliesv13(HttpRuntime.BinDirectory); //ASP.NET 

Issues when using ADO.NET to read SqlGeometry from Sql ServerDespite which version of Microsoft.SqlServer.Types.dllyou are using, if you try to read them from Sql Server using ADO.NETyou may encounter a cast exception because SQL Client will by default load version 10.0.0.0 of Microsoft.SqlServer.Types.dll. In this case some years ago I tried WKB (approach 1 and 2) and WKT as a medium to convert between SqlGeometrytype for different version of Microsoft.SqlServer.Types.dlland found WKB is about 10 times faster but some month ago I found using assembly redirection we can force the program to load the version we are using and using a simple cast we can get the SqlGeometry(approach 3)

使用 ADO.NET 从 Sql Server 读取 SqlGeometry 时出现的问题 不管Microsoft.SqlServer.Types.dll您使用的是 哪个版本,如果您尝试使用 ADO.NET 从 Sql Server 读取它们,您可能会遇到强制转换异常,因为 SQL 客户端将默认加载版本 10.0.0.0的Microsoft.SqlServer.Types.dll。在这种情况下,几年前我尝试过 WKB(方法 1 和 2)和 WKT 作为在SqlGeometry不同版本的类型之间转换的媒介Microsoft.SqlServer.Types.dll,发现 WKB 快了大约 10 倍,但几个月前我发现使用程序集重定向我们可以强制程序加载我们正在使用的版本并使用简单的转换,我们可以获得SqlGeometry(方法 3)

private List<SqlGeometry> SelectGeometries(string connectionString)
{
    SqlConnection connection = new SqlConnection(connectionString);
    var command = new SqlCommand(select shapeCol from MyTable, connection);
    connection.Open();
    List<SqlGeometry> geometries = new List<SqlGeometry>();
    SqlDataReader reader = command.ExecuteReader();
    if (!reader.HasRows)
    {
        return new List<SqlGeometry>();
    }
    while (reader.Read())
    {
        //approach 1: using WKB. 4100-4200 ms for hundred thousands of records
        //geometries.Add(SqlGeometry.STGeomFromWKB(new System.Data.SqlTypes.SqlBytes((byte[])reader[0]), srid).MakeValid());
        //approach 2: using WKB. 3220 ms for hundred thousands of records
        //geometries.Add(SqlGeometry.Deserialize(reader.GetSqlBytes(0))); 
        //approach 3: exception occur if you forget proper assembly redirection. 2565 ms for hundred thousands of records
        geometries.Add((SqlGeometry)reader[0]);
    }
    connection.Close();
    return geometries;
}

回答by Robson Douglas

Remove the Microsoft.SqlServer.Types.dll from References and use Nuget to instal. Check your version before install. The assemblies to x86 and x64 will be installed in the project.

从 References 中删除 Microsoft.SqlServer.Types.dll 并使用 Nuget 进行安装。安装前检查您的版本。x86 和 x64 的程序集将安装在项目中。

回答by John

I had the same issue in godaddy VPS with windows server 2012 r2

我在使用 windows server 2012 r2 的 Godaddy VPS 中遇到了同样的问题

I Resolved it by Updating my EF5 to EF6
I Resolved it by Updating my EF5 to EF6

in package manager console run to EF5 to EF lalest

在包管理器控制台中运行到 EF5 到 EF lalest

Install-Package EntityFramework 

回答by Adam

Despite having SQL Server 14.x installed, VS kept insisting SqlServerSpatial110.dll was not found.

尽管安装了 SQL Server 14.x,VS 仍然坚持认为未找到SqlServerSpatial 110.dll。

Installing Microsoft System CLR Types for SQL Server 2008 R2did not fix it. I also tried to install the 10.5 version of Microsoft.SqlServer.Types, but received a PInvoke error about the method signature not matching.

为 SQL Server 2008 R2安装Microsoft System CLR 类型并没有修复它。我还尝试安装 10.5 版本Microsoft.SqlServer.Types,但收到有关方法签名不匹配的 PInvoke 错误。

So instead, I installed Microsoft.SqlServer.Types14.x, then renamed the SqlServerSpatial140.dll file to SqlServerSpatial110.dll in both /x86 and /x64 folders and did the same in Loader.cs. For whatever reason, that seemed to do the trick.

因此,我安装了Microsoft.SqlServer.Types14.x,然后在 /x86 和 /x64 文件夹中将 SqlServerSpatial 140.dll 文件重命名为 SqlServerSpatial 110.dll,并在Loader.cs. 无论出于何种原因,这似乎都奏效了。

回答by Andrew Grothe

I've been having a similar issue on an ASP.NET MVC 5 project. A while back I had to add a line to specify the Assembly name like so:

我在 ASP.NET MVC 5 项目中遇到了类似的问题。不久前,我不得不添加一行来指定程序集名称,如下所示:

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
SqlProviderServices.SqlServerTypesAssemblyName = Assembly.GetAssembly(typeof(Microsoft.SqlServer.Types.SqlGeography)).FullName;

I recently deployed to a new test server and got this error again. It was trying to load version 12 for some reason. I now specify the exact version I want and it works as expected.

我最近部署到新的测试服务器并再次出现此错误。由于某种原因,它试图加载版本 12。我现在指定我想要的确切版本,它按预期工作。

SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";

Hope this helps someone.

希望这可以帮助某人。

回答by Fran?ois Breton

I had an old (2009) asp.net webform vb.net project that gave me this error on another server. I had to add this runtime to the web.config :

我有一个旧的(2009 年)asp.net webform vb.net 项目,它在另一台服务器上给了我这个错误。我必须将此运行时添加到 web.config :

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

http://biandintegration.blogspot.com/2017/12/solved-unable-to-load-dll.html

http://biandintegration.blogspot.com/2017/12/solved-unable-to-load-dll.html

回答by Fakhar Ahmad Rasul

Here is what was causing this issue for me:-

以下是导致我出现此问题的原因:-

There is a folder named SQLServerTypesin my project and when I looked inside the folder I found out that the .dll was actually missing. So downloading the .dll and pasting it inside the SQLServerTypesfolder fixed the issue for me.

SQLServerTypes我的项目中有一个文件夹,当我查看文件夹时,我发现 .dll 实际上丢失了。因此,下载 .dll 并将其粘贴到SQLServerTypes文件夹中为我解决了这个问题。

The issue was happening for me when I was trying to build my application.

当我尝试构建我的应用程序时,这个问题发生在我身上。