如何在 ASP.NET Core 1.0 应用程序中使用 SqlServer.Types/空间类型 [英] How to use SqlServer.Types / spatial types within ASP.NET Core 1.0 application

查看:15
本文介绍了如何在 ASP.NET Core 1.0 应用程序中使用 SqlServer.Types/空间类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的一个类库使用 Microsoft 空间类型,例如 DbGeography.在没有旧版本 SQL Server 和 Visual Studio 的干净机器上运行我们的应用程序时,我们会收到以下异常:

One of our class libraries uses the Microsoft spatial types like DbGeography. When running our application on a clean machine without older versions of SQL Server and Visual Studio, we get this exception:

空间类型和功能不适用于此提供程序因为程序集 'Microsoft.SqlServer.Types' 版本 10找不到或更高.

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

解决方案显然是安装这个 nuget 包:

The solution is apparently to install this nuget package:

Install-Package Microsoft.SqlServer.Types

安装后,nuget 包会提供有关如何从每个项目类型中引用 DLL 的说明:

After installing, the nuget package gives instructions on how to reference the DLLs from each project type:

要将使用空间数据类型的应用程序部署到未安装SQL Server 的系统 CLR 类型"的计算机,您还需要部署本机程序集 SqlServerSpatial110.dll.

To deploy an application that uses spatial data types to a machine that does not have 'System CLR Types for SQL Server' installed you also need to deploy the native assembly SqlServerSpatial110.dll.

此程序集的 x86(32 位)和 x64(64 位)版本都已添加到您的项目中的 SqlServerTypesx86 和 SqlServerTypesx64 子目录下.如果未安装 C++ 运行时,还包括本机程序集 msvcr100.dll.

Both x86 (32 bit) and x64 (64 bit) versions of this assembly have been added to your project under the SqlServerTypesx86 and SqlServerTypesx64 subdirectories. The native assembly msvcr100.dll is also included in case the C++ runtime is not installed.

您需要添加代码以在运行时加载正确的这些程序集之一(取决于当前架构).

You need to add code to load the correct one of these assemblies at runtime (depending on the current architecture).

ASP.NET 应用程序对于 ASP.NET 应用程序,将以下代码行添加到 Global.asax.cs 中的 Application_Start 方法:SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));

ASP.NET applications For ASP.NET applications, add the following line of code to the Application_Start method in Global.asax.cs: SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));

桌面应用程序对于桌面应用程序,添加以下代码行以在执行任何空间操作之前运行:SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

Desktop applications For desktop applications, add the following line of code to run before any spatial operations are performed: SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

nuget 包项目站点没有响应,所以我不确定这是 2016 年现在使用的最佳方法.

The nuget package project site is unresponsive, so I'm not sure this is the best approach to use now in 2016.

我的问题是,我不知道如何从 ASP.NET Core 1.0 应用程序调用 LoadNativeAssemblies.我们使用的是完整框架 (net461),而不是核心框架.

My problem is, I can't figure out how to call LoadNativeAssemblies from an ASP.NET Core 1.0 application. We're using the full framework (net461) and not the core framework.

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        ...
        SqlServerTypes.Utilities.LoadNativeAssemblies(env.WebRootPath);
        ...
    }
}

在 ASP.NET 1.0 应用程序中包含 SqlServer.Types dll 文件的最佳方式是什么?

What is the best way to include the SqlServer.Types dll files within an ASP.NET 1.0 application?

相关问题这里和<在 StackOverflow 上的 href="https://stackoverflow.com/questions/21887497/sqlserverspatial110-dll-failed-to-copy-on-windows-azure-deployment%5C">此处.

非常感谢.

推荐答案

我今天在一个 ASP.NET Core 应用程序上工作(下面是 .NET Framework,不是 .NET Core,与原始海报相同).

I got this working on an ASP.NET Core application today (.NET Framework underneath, not .NET Core, same as the original poster).

我注意到,将 Nuget 包直接添加到我的 ASP.NET Core 站点不会产生额外的文件.不确定这些包是否已更新为与 ASP.NET Core 一起使用?

The thing I noticed is that adding the Nuget package directly to my ASP.NET Core site yielded no additional files. Not sure if the packages were ever updated to work with ASP.NET Core?

无论如何,我只是将包添加到传统的 .NET Framework 4.x 项目中,并删除了它创建的 SqlServerTypes 文件夹,然后将该文件夹放在我的 ASP.NET Core 项目的根目录中.将下面所有 4 个 DLL 的 Copy to Output Dircectory 属性从 Do not copy 更改为 Copy Always,并添加了对 的调用LoadLibrary().

At any rate, I just added the package to a traditional .NET Framework 4.x project and ripped out the SqlServerTypes folder it creates, then put that folder in the root of my ASP.NET Core project. Changed the Copy to Output Dicrectory property for all 4 DLLs underneath from Do not copy to Copy Always, and added the call to the LoadLibrary().

值得注意的是,14.x版本的包实际上是SQL vNext,并没有出来,在我的脑海里应该已经被标记为预发布了.我坚持使用 13.x,因为我们使用的是 SQL 2016.

It's worth noting that the 14.x version of the package is actually SQL vNext which is not out, it should have been marked as a pre-release in my mind. I stuck with 13.x since we're using SQL 2016.

最初我将它放在 Program.cs 文件中,因为它与中间件、ServiceInjection 或托管配置设置没有任何关系.但是你必须从反射中获得路径,这感觉很难看.所以我把它作为 Startup 构造函数的第一行,因为从那里我可以使用 HostingEnvironment 路径.

Originally I had put this in the Program.cs file, as it didn't have anything to do with the Middleware, ServiceInjection or hosting Configuration setup. But then you have to get the Path to pass in from reflection, which felt ugly. So I put it as the first line of the Startup constructor instead, since from there I can use the HostingEnvironment path.

public Startup(IHostingEnvironment env)
{
    SqlServerTypes.Utilities.LoadNativeAssemblies(env.ContentRootPath);

    //The normal config as usual down here...
    var configuration = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
}

这篇关于如何在 ASP.NET Core 1.0 应用程序中使用 SqlServer.Types/空间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆