Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type 'Microsoft.SqlServer.Types.SqlGeography' exists in both 'Microsoft.SqlServer.Types.dll' and 'Microsoft.SqlServer.Types.dll'

Tags:

c#

sql-server

In my windows class library (consumed by a MVC website) I have installed the NugetPackage Microsoft.SqlServer.Types (Spatial).

Now, using ado.net I am trying to read the value by doing:

protected SqlGeography MapSqlGeography(DbDataReader reader, string key)
{
      return reader[key] is DBNull ? null : (SqlGeography)reader[key];
}

If I add a brake point in this line and in the visual studio watch window I type: "reader[key]", I can see the correct Point(XXXX,XXX) of type: "object {Microsoft.SqlServer.Types.SqlGeography}"

But, as soon as I try to make the cast I have the following error:

(SqlGeography)reader[key]   The type 'Microsoft.SqlServer.Types.SqlGeography' exists in both
 'Microsoft.SqlServer.Types.dll' and 
 'Microsoft.SqlServer.Types.dll'

Main strange fact is that the dlls are exactly the same...

As far as I know I only have one "source" for this namespace/class name, it should not be duplicated....

My "usings" are:

using Microsoft.SqlServer.Types;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks;

Any ideas on how to solve this? Thanks.

Update #1

I uninstalled the NugetPackage `Microsoft.SqlServer.Types (Spatial)' and instead tried the one called: 'Microsoft.SqlServer.Types (Unofficial)' and even after cleaning all the previous folders/files and also cleaning up the "bin/obj", I continue to have the exact same exception....

I simply do now see the cause of this now.... any ideas would be really appreciated.

Update #2

Just tried to use extern alias destination;

return reader[key] is DBNull 
        ? null 
        : (destination.Microsoft.SqlServer.Types.SqlGeography)reader[key];

And have the exception:

Cannot cast 'reader[key]' (which has an actual type of 'Microsoft.SqlServer.Types.SqlGeography') 
to 
'Microsoft.SqlServer.Types.SqlGeography'
Microsoft.SqlServer.Types.SqlGeography
like image 367
Dryadwoods Avatar asked Dec 02 '16 10:12

Dryadwoods


2 Answers

I encountered this error today because a referenced library included a different version of Microsoft.SqlServer.Types from Nuget than the locally installed one.

You can install a matching version using Nuget to resolve the issue, or you may be able to use binding redirects if that is not an option.

For example:

Install-Package Microsoft.SqlServer.Types -Version 10.50.1600.1

Check your specific versions by looking at package.json for your dependencies, or perhaps you can check the DLL properties directly.

like image 93
jocull Avatar answered Oct 09 '22 01:10

jocull


The runtime uses the following steps to resolve an assembly reference:

  1. Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.
  2. Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly.
  3. Checks the global assembly cache. If the assembly is found there, the runtime uses this assembly.
  4. Probes for the assembly using the following steps:
    • If configuration and publisher policy do not affect the original reference and if the bind request was created using the Assembly.LoadFrom method, the runtime checks for location hints.
    • If a codebase is found in the configuration files, the runtime checks only this location. If this probe fails, the runtime determines that the binding request failed and no other probing occurs.
    • Probes for the assembly using the heuristics described in the probing section. If the assembly is not found after probing, the runtime requests the Windows Installer to provide the assembly. This acts as an install-on-demand feature. Note There is no version checking for assemblies without strong names, nor does the runtime check in the global assembly cache for assemblies without strong names.

Now, check to see if you have multiple assemblies referenced (as Damien_The_Unbeliever said in the comment) or you do not have specific version set for that assembly.

Also you can try the Assembly Binding Log Viewer (Fuslogvw) to see exactly what gets loaded and what are the search paths.

like image 24
Mihail Stancescu Avatar answered Oct 08 '22 23:10

Mihail Stancescu