问题
We have a Portable Class Library targeting 4.0. A WPF application references this which is a standard .NET 4.0 app.
The PCL references the System.Net.NetworkCredential class.
When we build the .NET 4.0 app we get an error:
The type 'System.Net.NetworkCredential' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Net, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.
Several posts say to install KB2468871. That doesn't apply/change things.
If we add an assembly reference to "System.Net 4.0.0.0" in the app then the error changes:
Reference to type 'System.Net.NetworkCredential' claims it is defined in 'c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Net.dll', but it could not be found...
Of course NetworkCredential is not in System.Net, it is in the System assembly.
If I retarget to 4.5 everything works, but we need to use 4.0.
Any ideas? Thanks.
回答1:
KB2468871 enables portable libraries to run on 4.0, it does not enable them to build in Visual Studio.
The first error should go away if you add a reference to System.Net, 4.0.0.0. If it does not, then you're machine is missing a required patch.
If are using:
Visual Studio 2010
You need to install Portable Library Tools: http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/. This will install a patch that will make this work.
Visual Studio 2012 or Visual Studio 2013
This should work out of the box, if not, then you install is likely in an incomplete state. Try repairing your installation.
来源:https://stackoverflow.com/questions/19936503/portable-class-library-you-must-add-a-reference-to-assembly-system-net-versi