Why VS2012 project with referenced assembly can't target 4.0 automatically - reference

In a Visual Studio 2012 C# console application, I downgrade ".NET Framework Target" from 4.5 to 4.0. Win 7 Pro with both Frameworks installed.
I then reference an assembly, which, through warnings complains the following:
The primary reference "System.Threading.Tasks.Dataflow, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" could not be resolved because it has an indirect dependency on the framework assembly "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.0". To resolve this problem, either remove the reference "System.Threading.Tasks.Dataflow, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" or retarget your application to a framework version which contains "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
If I try to compile at this point, I error, becuase types and namespaces from referenced assembly aren't available, as if the assembly was not referenced at all.
The "Add Reference" dialog doesn't have any System.Runtime choice, but if I manually brose to
C:\Windows\Microsoft.NET\Framework\v4.0.30319\
and reference the System.Runtime assembly found there, warnings go away and I am able to compile.
Questions:
Is such forcing of the System.Runtime version a potential issue down the road (deployment).
If VS Project properties are seto to target Framework 4.0 (doesn't that relate to targeting the 4.0 SystemRuntime/CLR), why isn't the refferenced DLL picking that up and why manually adding the reference to my project fixes that issue?

Even though the library System.Runtime is inside the C:\Windows\Microsoft.NET\Framework\v4.0.30319\ directory, it is not part of the .NET 4.0 framework. .NET 4.5 is an in-place update of 4.0 and is installed in the same folder with the same version number.
Here is a screenshot that proofs that the library does not exist on a play .NET 4.0 installation:
You can also validate this by browsing to the C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework directory where you find the original assemblies for all installed framework versions. You will find the System.Runtime.dll as part of the .NETCore\v4.5 and .NETPortable\v4.5 subdirectories.
The reason that you can add the library to your project is that the runtime did not change between 4.0 and 4.5, so Visual Studio doesn't know or even care that the library you added manually is installed by 4.5. In this case the targetting in Visual Studio is only a filter that avoids that you accidentally add a 4.5 assembly to a project that targets 4.0.
Additional information:
Rick Strahl has a very good blog post on the topic with a more detailed analysis:
http://www.west-wind.com/weblog/posts/2012/Mar/13/NET-45-is-an-inplace-replacement-for-NET-40

Is such forcing of the System.Runtime version a potential issue
Yes, this just won't work. It works on your machine because you have 4.5 installed. Your program will crash and burn on a client machine that only has 4.0. Never add a reference from the Framework directory. It is rather sad that they are still around, they get too many programmers in trouble, but backwards compat is sacred.
The build system can only tell you that you have a problem when you use the reference assemblies. The ones shown in the Add Reference dialog, they are stored in c:\program files\reference assemblies and are not the same as the runtime assemblies. You know that works, you did get the warning. Which, in a somewhat clumsy way, told you that you program won't work on a machine that has 4.0. Don't ignore that warning, you really do have to target 4.5 to use that assembly. Hard requirement you cannot avoid.
why isn't the refferenced DLL picking that up
Because it refuses to build a program that cannot run. Feature, not a bug.

Related

Azure Function - Could not load file or assembly

Im running an Azure Function .NET Standard 2.0 and get following error:
An exception of type 'System.IO.FileLoadException' occurred in Function.dll but was not handled in user code
Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=9.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
The assembly file exist in the bin/debug folder. Been reading some threads about this but without a solution (https://github.com/Azure/azure-functions-core-tools/issues/322#issuecomment-352233979), anyone know what to do?
I'm using code from another .NET Standard 2.0 project but all my projects have a reference to Microsoft.WindowsAzure.Storage 9.2.0.0 and that nuget package installed.
Thanks!
You can add reference below to downgrade your storage reference explicitly.
<PackageReference Include="WindowsAzure.Storage" Version="8.6.0" />
See this issue comment.
This restriction (like many of the other assembly version restrictions we had with other extensions) is a temporary issue and will be addressed in upcoming preview releases.
This issue has been fixed in latest host 2.0.11888, but built cli(tools to run functions) is not available yet.
Update
2.0.11888 has been available to VS users.

Visual studio can not find a specific reference even though it exists

I am setting up a Sitecore project and when I try to view the website in the Browser I get the following error message:
Could not load file or assembly 'Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5' or one of its dependencies. The system cannot find the file specified.
I understand the error, however, I am not sure why it is being thrown. Since I have included this reference for this project. I want answers of how you have overcome this error. Thanks
This is likely because you have the wrong version of the Owin.dll.
Though Sitecore out of the box doesn't depend on the Owin.dll there are some third party modules that rely on this dll particularly when concerning authentication.
I would check that you have not overwritten another version of the dll when you published the Visual Studio project to the website.
If you have binaries that depend on different versions of assemblies you can use Assembly binding redirects to target the correct version of the dll.

The type or namespace could not be found

I'm currently trying to convert a WiX 3.5 custom actions project in Visual Studio 2008 to WiX 3.7 and Visual Studio 2012 and I'm getting the following exception:
The type or namespace name 'MyNamespace' could not be found (are you
missing a using directive or an assembly reference?)
The dll is definitely referenced and Visual Studio 2012 has no problem seeing the namespace. Everything under the namespace even pops up in Intellisense, but when I build it I'm getting this exception.
Anyone have an idea of what's going on here?
Additional Info:
The namespace I'm referencing is a .Net 2.0 library and the custom actions project is a .Net 2.0 project.
EDIT:
After further investigation, I'm getting this warning, which I'm guessing is the root of the problem:
The primary reference "MyNamespace, Version=8.5.1.20, Culture=neutral,
PublicKeyToken=f593502af6ee46ae, processorArchitecture=MSIL" could not
be resolved because it has an indirect dependency on the .NET
Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0"
than the version "2.0.0.0" in the current target framework.
Why is it trying to use mscorelib 4.0 when this is a 2.0 project?
So it turned out that the dll being reference, while compiled to target the .Net 2.0 framework, was being compiled with the 4.0 compiler. Switching the project to compile with the 3.5 compiler solved the problem.
This may be completely unrelated but I had a similar issue yesterday. I had accidentally copied a class file (.cs) from one solution into another solution. Clearly, I hadn't added the references to the other solutions.
In our case, while the referenced dll was targeting .Net 4.0, it had its own reference to a .Net 4.5.2 dll. The referenced 4.0 framework target led us to believe that 4.5 or 4.5.1 were sufficient to handle things, but they weren't—the referencing project had to be 4.5.2 to handle the dll and all its references.
This was clearly an error on the part of the dll maker.

Unit testing with portable library for Windows Phone 7 and 8

I currently have a VS2012 solution with a WP71 project and a WP8 project. Each of these projects reference a Portable Class Library project where I keep the view models. I also have a Unit Test project that references the PCL and contains tests for the view models.
The problem I'm having is when I compile the unit test project I get the following error:
error CS0012: The type 'System.Windows.Input.ICommand' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'
All of the projects, including the unit test project, reference the Portable.MvvmLightLibs NuGet package.
After looking around for this assembly I noticed that there are different versions, one for WP8, one for Silverlight 4, etc. Out of all these versions I'm not sure which to use.
The unit test project is targeting .NET Framework 4.5, so I added the System.Windows assembly for that version which works, but I then get the following error:
error : CA0001 : Could not resolve reference to System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes
Seeing as this is a code analysis error I'm tempted to simply ignore this and carry on, but I was wondering if anyone had any ideas on how to solve this or whether it is safe to ignore it?
So, I figured a way to solve the problem.
To get the test project to compile I had to add the System.Windows.dll assembly from:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71
But, adding it using the Add Reference dialog automatically adds the .NET 4.5 version even if I browsed directly to the folder. To fix this I opened the *.csproj for the test project and replaced the line:
<Reference Include="System.Windows"/>
with
<Reference Include="System.Windows">
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71\System.Windows.dll</HintPath>
</Reference>
This ensured that the 2.0.5.0 version was referenced instead of the .NET 4.5 version. The problem with this approach is certain types conflict with other assemblies, namely the System.dll, but seeing as I'm only testing view models from a Portable Class Library, I'm not all that bothered.
Ideally the test project would also be a Portable Class Library, but I've not got round to testing that.
One other issue was I tend to have warnings show up as errors when building in release mode, so this always fails because there will always be a warning complaining about naming clashes with System.dll.

Missing assembly in Visual Studio 2012 ( but it is there! Honest! )

I have a project using Reactive Extensions and ReactiveUI. I upgraded to the pre release version and I have the following line of code
OAPHCreationHelperMixin.ObservableToProperty(observable,
Function(x) x.Value
Visual studio tells me there is an error here on the method ObservableToPropertyin that the following assembly is missing
Error 9
C:\Users\phelan\workspace\WeinMeasEval\WeinMeasEval\Global\ObservableBinding.vb
Reference required to assembly
'System.Reactive.Interfaces,
Version=2.0.20823.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
containing the type 'System.Reactive.Concurrency.IScheduler'. Add one to your project.
Now I click the link which suggests it will add the reference and then nothing
happens. If I look in my project references I see that the assembly is indeed
in the assembly list and it is the correct version 2.0.20823.0 and if I look on
the path the DLL is indeed there.
Any ideas?
This is now fixed, check out ReactiveUI 4.0 Preview 5
It turns out that at this date reactive UI is not built yet for .NET 4.5 as the developer is having some build issues.
As a work around I build it myself by pulling the repository from Git and referring to the core library directly from within my .NET 4.5 project.
This seems to work at the moment. YMMV
Do you have Target Framework set to full .net or to Client Profile (in project settings)?

Resources