JTech
JTech

Reputation: 3570

.Net adds the wrong version of a referenced assembly to the .csproj file

We're developing a DLL, call it "My.dll", which has a need to reference an older version of a .Net Framework DLL (older than what the application targets).

That is:

Application's (My.dll's) Target Framework = ".Net Framework 4"

We need to reference System.Web.Mvc v3.0.0.0 (to be compatible with an external host application)

Setting the Applications' (My.dll's) Target Framework to anything other than 4 breaks the code.

So, we simply copied the System.Web.Mvc v3.0.0.0 DLL to the projects 'lib' folder and added a reference to that assembly directly.

The version of the referenced MVC assembly as displayed by Visual Studio is "v3.0.0.0"...awesome!

enter image description here

However, after adding the reference, the Project File ends up with a reference to "v4.0.0.1"...WTF!?

<Reference Include="System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>lib\System.Web.Mvc.dll</HintPath>
</Reference>

And confusingly, the manifest of the compiled My.Dll references v3.0.0.0:

enter image description here

Then, when we deploy the application, we find (see Fusion Log Viewer below) that it loads System.Web.Mvc from the GAC and uses v4.0.0.0, even though v3.0.0.0 is present in the GAC.

Where is the disconnect happening?

Is Visual Studio ignoring the version I've given it when writing the entry in the .CSPROJ file and just taking the one that matches the Application's Target Framework???


Fusion Log Viewer Log

Assembly Binder Log Entry  (12/07/2017 @ 12:17:55 PM)

The operation was successful.

Bind result: hr = 0x0. The operation completed successfully.
[...OTHER LOG STATEMENTS OMITTED FOR BREVITY...]

LOG: Publisher policy file redirect is found: 4.0.0.1 redirected to 4.0.0.1.
LOG: ProcessorArchitecture is locked to MSIL.
LOG: Post-policy reference: System.Web.Mvc, Version=4.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_4.0.0.1__31bf3856ad364e35\System.Web.Mvc.dll.
LOG: Assembly is loaded in default load context.

Upvotes: 2

Views: 2453

Answers (1)

Konop
Konop

Reputation: 46

I suspect that Visual studio is showing correct version of the dll you're refering to, but in csproj there is a version from GAC or some other place.

Here is what worked for me:

  1. Go to csproj (in Visual studio).
  2. On reference set "Specific version" to true.
  3. Save all.
  4. On the same reference set "Specific version" back to false. (or leave it true if that's what you need).

In CsProj you should have correct version now. Eventualy you can just manually edit csproj file and set correct version.

Upvotes: 1

Related Questions