如何在ASP.NET Core 1.0应用程序中使用SqlServer.Types/spatial类型

End*_*050 16 asp.net entity-framework-6 asp.net-core asp.net-core-1.0

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

空间类型和函数不适用于此提供程序,因为无法找到程序集"Microsoft.SqlServer.Types"版本10或更高版本.

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

Install-Package Microsoft.SqlServer.Types
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

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

nuget包项目网站没有响应,所以我不确定这是2016年最好的方法.

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

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        ...
        SqlServerTypes.Utilities.LoadNativeAssemblies(env.WebRootPath);
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

相关的问题在这里,并在这里在计算器上.

非常感谢.

Nic*_*cht 5

我今天在 ASP.NET Core 应用程序上进行了这项工作(下面是 .NET Framework,而不是 .NET Core,与原始海报相同)。

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

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

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

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

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();
}
Run Code Online (Sandbox Code Playgroud)


LeR*_*Roi 0

这似乎对我有用:

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            ...
            SqlServerTypes.Utilities.LoadNativeAssemblies(env.WebRootPath + @"path to the project that contains the SQLServerTypes folder");
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)

我注意到它IHostingEnvironment.WebRootPath返回指向 wwwroot 的路径,但是在我的解决方案设置中,我在该文件夹中有多个项目,因此只需告诉它指向哪个项目即可帮助我。抱歉,如果这没有帮助。