The answers above didn't work for me, so I did a bit more digging and am sharing my findings here.
Summary: There was a change in the Microsoft SQL Server System CLR Types (SQLSysCLRTypes.msi
) library between SQL Server 2012 SP2 (11.0.2100.60) and SP3 (11.0.6020.0) and this problem can be fixed by upgrading this package and any stray DLLs to the latest version (corresponding to 2012 SP4 / 11.0.7001.0 at time of writing).
There are really only two things in this package:
Microsoft.SqlServer.Types.dll
- the .NET wrapper library
SqlServerSpatial110.dll
- the native library containing the spatial functionality
Note that myriad versions of SQLSysCLRTypes.msi
are available, corresponding with every major/minor release of SQL Server, but annoyingly they are all published with the same filename and unless you perform a full SQL Server installation then they tend to be manual prerequisites for installing things from the SQL Server Feature Pack (for example, see https://www.microsoft.com/en-us/download/details.aspx?id=56041)
From the SQL 2012 SP3 version of the package onwards, SqlServerSpatial110.dll
exports the function SetClrFeatureSwitchMap
, which is called from somewhere within the .NET wrapper DLL. Prior to SP3, that function didn't seem to exist and the .NET wrapper didn't try to use it. (you can list DLL exports using dumpbin /exports <dll file>
)
If the CLR Types MSI package is installed on a particular machine, and a different minor version of those DLLs is in your .NET program's working directory, then you can get the error. This could easily happen if you distribute your program with its dependency libraries to avoid extra installation steps for the end-user.
Whenever .NET libraries are installed to the system and included in the Global Assembly Cache (GAC), the system version will always be loaded by a .NET program even if a "local" copy can be found in the working directory. For native libraries, the working directory copy is used first. This means that when you reference Microsoft.SqlServer.Types
in your application and have both DLLs of matching versions in your application directory, if Microsoft.SqlServer.Types
is installed on the system with the same major version (ie. 11.0.0.0), then it can have problems when it tries to load its native library dependencies and gets an older version of SqlServerSpatial110.dll
from the working directory instead of the correct version from wherever it may be installed on the system.
How to Fix: Make sure any copies of SqlServerSpatial110.dll
have the same minor version as any copies of Microsoft.SqlServer.Types.dll
, and make sure you have the latest version of each. This probably only applies to SQL Server 2012 but it is possible that similar problems could occur in newer versions of SQL Server with eventual Service Pack releases.
Note that setting "Specific Version" to "True" for references to Microsoft.SqlServer.Types
(in Visual Studio) doesn't have an effect, since all the SQL Server 2012 CLR Types library versions expose the same version number to .NET (11.0.0.0), regardless of which service pack they are from.
References: