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

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


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.