C#, Shared Assemblies, publisher policy not working, MS VS .NET 2013 - c#

I can't figured out how to use a publisher policy correctly and I seem to have an similiar issue as related to:
How to make Publisher Policy file redirect assembly request
I've created shared assemply named "SharedAssembly.dll" in version "1.0.0.0", and then in version "2.0.0.0". Both are instaled in GAC (MSIL_GAC, target is .NET Framework 4.5).
I've created following publisher policy file *.xml (redirecting to an older version "1.0.0.0" is on purpose behaviour):
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity
name="SharedAssembly"
publicKeyToken="89f6ea550599ca14"
culture="neutral"/>
<bindingRedirect oldVersion="2.0.0.0"
newVersion="1.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Then I've created the publisher policy assembly using the command:
al /link:App.config /out:policy.2.0.SharedAssembly.dll /keyf:F:\VisualStudio\klucz.snk /v:1.0.0.0 /platform:anycpu
Of course, I installed it into GAC.
Then I created a new Console Application, I added reference to the "SharedAssembly.dll", but it doesn't work :(
It still connects with the 2.0.0.0 version of my shared assembly.
I tried many advices, as using fuslogvw.exe (no errors shown), getting rid of the "culture" attribute, etc., but it doesn't help.
I think the problem is not that I want to point to the older version of .dll?
Please help me to fix that issue, cause I've tried so many things and yet I can't make it work.

Related

Build environment CodeAnalysis task could not be instantiated

I'm trying to use Microsoft.Build.Evaluation.Project to run MSBuild inside my project.
I'm getting this error, and I can't figure out why, or where to start diagnosing it:
C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets:
error MSB4127: The "CodeAnalysis" task could not be isntantiated from
the assembly "C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\.\FxCopTask.dll".
Please verify the task assembly has been built using the same version of the
Microsoft.Build.Framework assembly as the one installed on your computer and
that your host application is not missing a binding redirect for
Microsoft.Build.Framework. Unable to cast object of type
'Microsoft.Build.Tasks.CodeAnalysis' to type
'Microsoft.Build.Framework.ITask'.
I've tried using ToolsVersion="12.0" in my build file
I've checked Microsoft.CodeAnalysis.targets and ensured FxCopTask.dll works
I've removed extraneous references and am now only referencing Microsoft.Build and Microsoft.Build.Framework
I've used MSBuild.exe in a shell process successfully on the same solution
This question seems to address the problem, but it's using the command line.
I'm not sure how to "verify the task assembly" or add "a binding redirect for the Microsoft.Build.Framework." Can someone point me in the right direction?
The answer here lay in obfuscated references. The default Microsoft.Build references are v4.0.
Turns out to reference the newest versions (at least in VS 2013 Community) I had to browse to the assemblies in C:\Program Files (x86)\MSBuild\12.0\bin and manually add the references. Success!
Thanks to BuildManager to use another version of MSBuild for pointing out this solution.
'Adding a binding redirect' is adding the following to your app.config file:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="12.0.0.0"/> <!--verify version!-->
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
For me, this resolved the issue. My error started with The "csc" task could not be instantiated, though.

Use a specific version of a dll in C#

I have a project where I would want to use some specific version of a dll.
The GAC contains couple of versions of that dll (new & old), I would want to use the old when running the program.
Issue is that the newest dll is always picked-up from the GAC.
Would you know if there is a way to either:
Force the usage the dll that is in the run folder (the one I'm referencing in my solution, working fine in debug).
Force the usage of the old version of the dll from the GAC.
Thank you!
You can use a binding redirect in your app.config or web.config in the runtime node:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Make sure you have the correct publicKeyToken and know which versions you want to redirect to what version.
(You can check a publicKeyToken of a DLL like this with this info.)
MSDN Documentation
You can also generate these for an entire solution using the Package Manager Console
Get-Project -All | Add-BindingRedirect
This will update all app.config files and add the binding redirect.
When you have added the library to your project and you collapse the 'References'-node of the project tree, you'll see the added library. When you select it and click the 'Properties'-node of the context menu, you can specify if a specific version of the library should be used and which version to use. Simply set 'Specific Version' to true and specify the Version number. Then you don't have to cope with the question where the version you want is loaded from.
Have you try to "Redirecting Assembly Versions" in your app.config? http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.110).aspx

Could not load file or assembly 'ABC.dll, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies

We have an C# class library project which was created using .Net framework 3.5.In that C# project we add an assembly ABC.dll build using VS 2008 3.5 framework with version 5.5.0.0.
Now when we add ABC.dll with an updated version of 6.6.2.1 then in Designer view still get an error:
Could not load file or assembly 'ABC.dll, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I am really stuck and cannot find the reason in-spite of adding 6.6.2.1 why the project is asking for 5.5.0.0 version dll?
We are now using C# Express Edition 2010 to open the project.
Make sure that the reference is present and any dependent assemblies are referenced appropriately; you may need to make sure that the references have the property "SpecificVersion" set "false".
If the assemblies are present and the correct version - try cleaning the solution (Build | Clean Solution), as sometimes old versions linger in the project output folders and confuse Visual Studio.
You should be able to add an app.config file, telling your library which file to use when the .dll is called.
This should contain something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ABC"
publicKeyToken="8fe83dea738b45b7"
culture="neutral"/>
<bindingRedirect oldVersion="5.5.0.0"
newVersion=" 6.6.2.1"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

NAnt error using solution task to compile .Net framework 3.5 project: Error MSB4127, Error MSB4060

I run into the error, listed below, when executing a NAnt task that would compile a .Net 3.5 project- with CC.Net:
[solution] C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): Error MSB4127: The "EntityDeploy" task could not be instantiated from the assembly "C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Data.Entity.Build.Tasks.EntityDeploy' to type 'Microsoft.Build.Framework.ITask'.
[solution] C:\WINDOWS\Microsoft.NET\Framework\v3.5\Microsoft.Data.Entity.targets(40,5): Error MSB4060: The "EntityDeploy" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.
[solution] Project 'CAPS.UnitTests.NETVersion3' failed!
[solution] Continuing build with non-dependent projects.
I quick research points to tha fact that I would need an assembly reference in my app.config file as:
The references in my project has a "Runtime Version" attribute value of v2.0.50727
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
However the error still persists in my case.
Cheers.
From NAnt documentation for the <solution> task:
Right now, only Microsoft Visual Studio .NET 2002 and 2003 solutions
and projects are supported. Support for .NET Compact Framework
projects is also not available at this time.
Currently there seems to be work in progress with NAnt 0.91 Release Candidate 1. Anyway I would advise to build solutions via MSBuild call from NAnt. You can do this by using either NAnt's <exec> task or NAntContrib's <msbuild> task. Find more information here, here, and here.

How can you make a c# project ignore the version number of an assembly?

I am working on a project that references dlls from another product. The product has a release each year and the assemblies version changes for each one, although the methods stay the same.
When I run a build of my project for 2010 when I try and run it for 2009 it throws an error because it is dependent on a different version. Is there a way around this?
If you're referring to a problem at runtime after swapping versions of your assembly without performing a rebuild of the program referencing your newly built assembly, you'll want to use a <bindingRedirect> directive to your program's App.config (or Web.config, if you're talking about a web site.)
bindingRedirect is used to instruct the .NET Framework that it's OK to use a version of an assembly other than the one the application was originally compiled against. By default, the CLR wants to see the same version of a DLL that your application was referencing during build, and if it doesn't it will throw an exception.
Try selecting the reference, and in property window set Specific Version as false.
It is possible to map different .net version of assembly in app.config that you put in application root folder
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Waters.ACQUITY.Remote"
publicKeyToken="6c13fd0b3604de03"
culture="neutral" />
<bindingRedirect oldVersion="1.40.0.0"
newVersion="1.60.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This is solution when assembly you have referenced has references inside it to another specifc library version.
It happens when at compilation time "Specific version" is set to true. To avoid this problem it should be false.

Categories