ObservableCollection<T> exists in both 'System.Windows' and 'System' - c#

After creating a new WPF project in VS 2017 targeting .NET 4.6 as soon as I try to use ObservableCollection<T> in my code I get the following error which prevents the app from building:
ObservableCollection exists in both 'System.Windows' and 'System'
In my projects I created in VS 2015 I have referenced both of these assemblies and the application worked just fine. I tried deleting contents of app's bin and obj folder, restarting VS and performing Build > Clean Solution but that did not help.
Removing reference to System.Windows allows application to build but that adversely affects other code (for example RaiseCanExecuteChanged of my DelegateCommand requires a reference to System.Windows).
Any idea why this is happening and how to fix it?
UPDATE Here is the full error message:
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'ObservableCollection' exists in both
'System.Windows, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' and 'System,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

The problem was that ReSharper was adding a reference to:
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETPortable\v4.0\Profile\Profile158\System.Windows.dll
instead of:
C:\Program Files (x86)\Reference
Assemblies\Microsoft\Framework.NETFramework\v4.6.2\System.Windows.dll
Once I removed the reference added by ReSharper and manually added the correct reference the problem was resolved.
ReSharper was detecting that DelegateCommand implementation of ICommand used by my MVVM framework needed a reference to System.Windows but was erroneusly adding a reference to System.Windows found in the NETPortable instead of System.Windows found in .NET...

Related

The type or namespace name 'Optimization' does not exist in the namespace

I am running into an issue where The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' when it was already installed(I checked Nuget Package Manager)
What I have tried:
1: Directly copy dll to the Bin folder because I notice it was missing. This did not work.
2: Uninstall and re-install it from Nuget Package Manager, after that, the same error still exist. On top of that, I got a new error
The pre-application start initialization method Start on type
System.Web.Optimization.PreApplicationStartCode threw an exception
with the following error message: Could not load file or assembly
'Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
system cannot find the file specified..
Package Version: 1.1.3
It is a WebForm Project, not MVC(there are many posts have solutions that's unique to MVC, so it is not applicable in my case)
I have installed both packages with the highest stable version, so I am not sure why Infrastructure is yelling at me.
I have fortunately found the solution:
Web.Optimization requires Web.Infrastructure. When I reinstall Web.Optimization, it changed the targetFramework from 40 to 45, while Infrastructure is looking at 40. Also, despite the fact that I have infrastructure installed, the dll is also missing. I copied net40 version of dll for both Web.Optimization and Web.Infrastructure and then it worked.

Fixing "They type 'X' exists in both 'Y' and 'Z'" C# error for System.ServiceModel in LINQPad

If I:
Create a new C# query in LINQPad 6.
Add the System.ServiceModel.Http NuGet package or another package that references it.
Try to instantiate a class from the System.ServiceModel namespace, for example System.ServiceModel.BasicHttpBinding.
Leading to the following .linq file:
<Query Kind="Expression">
<NuGetReference>System.ServiceModel.Http</NuGetReference>
</Query>
new System.ServiceModel.BasicHttpBinding()
Then I get a compile error:
CS0433 The type 'BasicHttpBinding' exists in both 'System.Private.ServiceModel, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.ServiceModel.Http, Version=4.7.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
If I create a project in Visual Studio and add the same NuGet package and code, I do not get this error.
According to the C# Language reference for the error, it should be possible to resolve by using the -reference compiler option or by not referencing one of the assemblies. However, I can't seem to find a way to use this compiler option in LINQPad, nor can I find any way to remove the assembly reference to System.Private.ServiceModel.
How can I fix the error?
This is a bug in LINQPad, triggered by an obscure scenario. The System.Private.ServiceModel package contains a lib folder with an assembly which is required at runtime, and a ref folder with a underscore.underscore file which indicates that no assemblies should be referenced by the compiler. Because LINQPad finds no reference assemblies, it feeds the compiler the assembly in the lib folder, which causes the error.
I've got a fix ready and regression tests are currently running. The fix will likely make it into the 6.11.2 beta build, which should be released in a day or two.

Reference Collections.Immutable in visual studio extension

I have a visual studio extension (for VS 2017) that uses Roslyn in some functions and references System.Collections.Immutable, Version=1.2.2.0. When I first ran it, it would complain that it didn't have a reference to the System.Collections.Immutable assembly, so I added it the assets in the source.extensions.vsixmanifest (along with Microsoft.CodeAnalysis.CSharp.dll and Microsoft.CodeAnalysis.dll as it happens).
This fixed my problem with the missing assembly, but now I get:
Method not found: 'System.Collections.Immutable.ImmutableArray`1 Microsoft.CodeAnalysis.Scripting.Script.Compile(System.Threading.CancellationToken)'.
I believe it might still be referencing a different version of System.Collections.Immutable, while debugging the extension, if I try this:
System.Reflection.Assembly.GetAssembly(typeof(System.Collections.Immutable.ImmutableArray))
I get:
error CS0433: The type 'ImmutableArray' exists in both 'System.Collections.Immutable, Version=1.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
Is there a way to resolve this within the extension? (things like .config assembly redirects don't really work well unless you edit Visual Studio's own .config, which I 'd prefer to avoid)

Failed to build UWP project with Visual Studio 2017 due to missing reference

I am trying to build a UWP project using Visual Studio 2017.
Without success :(
Here is the error messages content:
error CS0012: The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncActionWithProgress<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0012: The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
error CS0103: The name 'Package' does not exist in the current context
The code lines inducing the errors all contain the await keyword and are like that:
var storageFolder = Task.Run(async () => await StorageFolder.GetFolderFromPathAsync(folderPath));
The "Quick actions" propose me to Add reference to 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
When I try, I have, in the Reference Manager' dialog :
The Universal SDK is already referenced. Please us the Object Browser to explore the references in th Universal Windows SDK.
And
No Framework assemblies were found on the machine
I un-installed VS and re-install => nothing changed
I un-installed all features/applications referencing .net and visual Studio and re-install VS => nothing changed
I tried installing the Windows 10 SDK (10.0.14393) before or after installing VS => nothing changed
I really have no idea of what goes wrong with my dev environment.
Do you know what the error messages mean ?
And does someone can help me to fix it?
TIA
I didn't know how I fixed the problem.
I "played" with the emulator extensions, especially the Android one.
Without understanding, while uninstalling it, my problem disappears.
Sorry, for the ones who will encounter the same problem I did, I close the discussion as my problem has been fixed, even if I can't explain why.

CS1705 error while adding a C++ project to solution

Hi and thanks in advance.
I'm using TFS build 2013 (team services, not on premise) to build my solution. After creating a new C++ project in the solution I'm unable to build successfully.
The error I'm getting on the build machine is:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis\Microsoft.CodeAnalysis.targets
(219): An error has occurred during compilation. error CS1705: Assembly 'Microsoft.Build.Utilities.Core,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' uses 'Microsoft.Build.Framework,
Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher
version than referenced assembly 'Microsoft.Build.Framework, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis\Microsoft.CodeAnalysis.targets
(219): The "SetEnvironmentVariable" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name
of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask
interface. 3.) The task is correctly declared with <UsingTask> in the project file, or
in the *.tasks files located in the "C:\Program Files (x86)\MSBuild\14.0\bin\amd64" directory.
I've red about error CS1705 on the internet but I still don't understand why I get this error - the project is an empty project which has no references what so ever and no other project reference it either.
I'm targeting .NET framework 4.6.1 on all my C# projects and in my build definition I'm adding these msbuild arguments: /tv:14.0 /p:VisualStudioVersion=14.0 hence it uses the 2015's version of the c# compiler.
The target platform for this C++ project is VS 2015 (v140) as follows:
C++ project configuration
Needless to say that when I'm removing this project from the list of project to build (configuration manager) - the solution builds successfully.
Any help/clue would be greatly appreciated!
The problem was something other than what I thought it was. Seems it's got something to do with CodeAnalysis and some environment variable related to it.
In my build definition I'm disabling CodeAnalysis and even inside the C++ project properties It's explicitly disabled. After I commented out some attributes on the "SetCABuildNativeEnvironmentVariables" target inside this file on the build machine:
"C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeAnalysis\Microsoft.CodeAnalysis.targets"
The problem disappered.
This is a nasty workaround however since I'm not fully aware of what's really happening.
Thanks a lot anyways!
Seems there are something still point to "Microsoft.Build.Framework, Version=12.0.0.0"
The /tv:14.0 command argument doesn't work as expected. See:
BuildActivity ignores ToolsVersion
As a workaround, you need to customize the build process template:
Open the template in Visual Studio and find the Run MSBuild for
Project MSBuild activity.
Set ToolVersion to "14.0".
Set ToolPath to target to MSBuild14 (by default: "C:\Program Files
(x86)\MSBuild\14.0\Bin").
Check in this build process template and re-queue the build.
You can also refer the answer from Marson in TFS 2013 building .NET 4.6 / C# 6.0

Categories