2
votes

I'm trying to write some unit tests, using NUnit, for code that relies on DbGeography.PointFromText(text, 4326), which requires Microsoft.SqlServer.Types be loaded. I'm trying to load it using:

[OneTimeSetUp]
public void Startup()
{
    SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
}

I have also tried to use load them in a [Setup] method. When the code that uses DbGeography is called, I end up with the lovely exception:

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

I do have the Microsoft.SqlServer.Types Nuget package installed, and it is working fine and dandy in my Web API 2.2 application.

2
Is the nuget pkg installed in your test project as well? - CodingYoshi
Yes, it is installed in the test project, too. - Jereme

2 Answers

0
votes

I ended up installing the Microsoft SQL Server CLR types, and my unit tests were able to find the necessary types.

0
votes

You can install the libraries on the system, but then will also need to remember to install on build servers and production, etc.

Alternatively, have had success with doing the install of the Microsoft.SqlServer.Types NuGet and loading the assemblies as instructed. For xunit I included this in a TextFixure ctor.

        SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);

Also, found I needed to add bindingRedirects:

  <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>