How to use SqlServer.Types / spatial types within ASP.NET Core 1.0 application

偶尔善良 提交于 2019-11-30 03:55:01

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

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?

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().

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.

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();
}

i fix this with a bindingRedirect in web.config file.

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

im using SQL 2016, ASP.NET (no core) and EF 6.0.0.0

this seemed to work for me:

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

I notice that IHostingEnvironment.WebRootPath returns the path that points to the wwwroot however in my solution setup I have multiple projects within that folder, so just telling it which project to point to helped me. Sorry if this doesn't help.

I fixed it, its not a clean fix, indeed it is everything but clean:

I had to create a folder in /bin named packages with the sql server types package... And I had to do it in every project referenced by the asp net core one that was using sql server types so it seems to be a bug or a missbeheviour in the targets files.

Note that this workaround will have to be done by everyone in your team because /bin content won't be under source control, as it's expected.

I hope it helps (meanwhile it is fixed)

(Well, It's too late, tomorrow I'll think a bit more on what caused the problem and how I fixed it and if I find a better workaround I'll let you know, by know it builds and run as expected)

Using .net core 2.0. After installing the nuget package mentioned above I put this in Program.cs before the line with BuildWebHost(args).Run(); and it worked..

Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory); SqlProviderServices.SqlServerTypesAssemblyName = "Microsoft.SqlServer.Types, Version=14.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91";

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!