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

编程入门 行业动态 更新时间:2024-10-19 17:31:11
本文介绍了如何在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位)版本均已添加到您的项目中的SqlServerTypes \ x86和SqlServerTypes \ x64子目录下.如果未安装C ++运行时,还包括本机程序集msvcr100.dll.

Both x86 (32 bit) and x64 (64 bit) versions of this assembly have been added to your project under the SqlServerTypes\x86 and SqlServerTypes\x64 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?

相关问题此处和此处.

非常感谢.

推荐答案

我今天在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 Dicrectory属性从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,但尚未发布,在我看来应该已经将其标记为预发行版本.由于我们使用的是SQL 2016,因此我坚持使用13.x.

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/空间类型

本文发布于:2023-11-14 19:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   类型   如何在   空间   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!