Newtonsoft.Json version in NuGet (version 7.0.1) gives the below error message when running code analysis.
CA0001 : Could not resolve reference to mscorlib, Version=2.0.5.0, ...
The error occurs in portable class library that targets .Net 4.5, Windows 8 & ASP.NET Core 5.0
The error does not occur if the portable class library targets .Net 4.6, Windows Universal 10 & ASP.NET Core 5.0
I took Newtonsoft.Json source code and compiled to portable class library targeting .Net 4.5, Windows 8 & ASP.NET Core 5.0. Then I referred to the assembly I compiled instead of the NuGet package. The code analysis problem does not happen in this scenario.
Note that I use Visual Studio 2105 running on Windows 10. Targeting .Net 4.6 is not an option for me due to other dependencies.
Please let me know if there is a good way to make the NuGet package work for the particular PCL target I need and code analysis.
You can avoid the error by adding a CodeAnalysisAdditionalOptions /assemblyCompareMode:None to your .csproj file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
....
<CodeAnalysisAdditionalOptions>/assemblyCompareMode:None</CodeAnalysisAdditionalOptions>
....
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
....
<CodeAnalysisAdditionalOptions>/assemblyCompareMode:None</CodeAnalysisAdditionalOptions>
....
</PropertyGroup>
....
</Project>
It seems the bug was fixed in Version 9.0.1.
Related
I have few projects in my solution - few for mobile (Xamarin.Forms) and one xUnit. xUnit is build with .NET5 and references mobile project.
There are 2 pipelines on Azure DevOps to build mobile app. One is for Android and runs on Windows, another for iOS and runs on OSX.
xUnit project build started to fail after I added new NuGet to mobile project (Rg.Plugins.Popup).
Initially, setting the TargetFramework for xUnit project looked like this
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
That setup worked on OSX, but did not work on Windows with error message Error NETSDK1136: The target platform must be set to Windows (usually by including '-windows' in the TargetFramework property) when using Windows Forms or WPF, or referencing projects or packages that do so.
Changing it to
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
fixes Windows problem, but it starts failing on OSX (with same error message, which is weird, as we are no longer on Windows).
I tried few other options, but could not make it to succeed on both machines.
<PropertyGroup>
<TargetFramework Condition=" '$(OS)' == 'Windows_NT' ">net5.0-windows</TargetFramework>
<TargetFramework Condition=" '$(OS)' != 'Windows_NT' ">net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>net5.0;net5.0-windows</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
I also tried with Choose, When and Otherwise, but it looks like you cannot wrap TargetFramework into that at all.
Am I doing it completely wrong or it it just some coincidence that correct code does not work in my case?
Try to place Condition inside PropertyGroup not TargetFramework .
Try the code below
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
<TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
</PropertyGroup>
When compile or debug a package project (.wapproj) with inside a Xamarin Form UWP application, recevive this error:
XF requires .NETFramework >= v4.6.1. You have 'v4.5.1'
Add this on .wapproj
<Project ...>
...
<PropertyGroup>
<XFDisableFrameworkVersionValidation>True</XFDisableFrameworkVersionValidation>
</PropertyGroup>
...
</Project>
Last VS 2019 update 16.9.2 resolve this issue for me.
Try it.
Carbonete
I'm developing .net core console application. I want to alert to user when want to exit application. Like below;
MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
Application.Exit();
But I can't add System.Windows.Forms referance to the my project. I'm getting this error.
Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
Is that possible to show Message Box on .net core Console Application?
In order to use Windows Forms you need to modify .csproj:
set UseWindowsForms to true
append -windows to TargetFramework (e.g. net6.0-windows)
ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Program.cs:
System.Console.WriteLine("Hello, Console!");
System.Windows.Forms.MessageBox.Show("Hello, Popup!");
Result:
Notes:
I checked this solution on .NET Core 3.1, .NET 5 and .NET 6 on Windows x64.
If you don't want console window to be shown, set OutputType in .csproj to WinExe instead of Exe.
Windows Forms works only on Windows, so as this solution.
On .NET Core 3.1 there is no requirement to change target framework to Windows, however it is still impossible to publish executable for Linux OS.
If you need cross-platform solution that shows popups on Windows and only use console on Linux – you can create custom build configuration and use preprocessor directives as in the example below.
Cross-platform ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WindowsDebug;WindowsRelease</Configurations>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'WindowsDebug' Or '$(Configuration)' == 'WindowsRelease'">
<DefineConstants>WindowsRuntime</DefineConstants>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Cross-platform Program.cs
System.Console.WriteLine("Hello, Console!");
#if WindowsRuntime
System.Windows.Forms.MessageBox.Show("Hello, Popup!");
#endif
Some console apps need this line added to the project as well...
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
Adding this to the example in Daniil's post would give you this...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<!--Insert DisableWinExeOutputInference here -->
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
</Project>
I am working on a Visual Studio 2017 Solution that contains 3 projects:
Two Class Libraries in .Net Standard 2.0 (Any CPU)
One ASP.Net in .Net Framework 4.6.1 (Any CPU)
If I Build All in Debug (Any CPU), all runs fine.
But if I Build All in Release (Any CPU), then this error shows in the Output Window:
3>SGEN : error : An attempt was made to load an assembly with an incorrect format: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\ref\netfx.force.conflicts.dll.
How to solve it?
The error stems from confusion between NETStandard and NuGet libraries when resolving dlls. Put this into your failing project's .csproj file (Unload project, Edit .csproj file):
<Target Name="ReplaceNetFxNetStandardRefWithLib" AfterTargets="ImplicitlyExpandNETStandardFacades">
<ItemGroup>
<Reference Remove="#(_NETStandardLibraryNETFrameworkReference)" Condition="'%(FileName)' != 'netfx.force.conflicts'" />
<Reference Remove="#(_NETStandardLibraryNETFrameworkReference)" Condition="'%(FileName)' != 'System.Configuration.ConfigurationManager'" />
<Reference Include="#(_NETStandardLibraryNETFrameworkLib)">
<Private>true</Private>
</Reference>
</ItemGroup>
</Target>
<Target Name="RemoveNetFxForceConflicts" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<ReferencePath Remove="#(ReferencePath)" Condition="'%(FileName)' == 'netfx.force.conflicts'" />
<ReferencePath Remove="#(ReferencePath)" Condition="'%(FileName)' == 'System.Configuration.ConfigurationManager'" />
</ItemGroup>
</Target>
In my case, this error was reported due to incorrect package installed in the project. Though you uninstall the incorrect package installed it was still reporting error, the solution will be
Ensure to uninstall the package with the option "Force uninstall even if there are dependencies on it"
Even after following step 1 if you still encounter the same sgent error, then expand "References" node in solution explorer, remove the unwanted dlls that might have been installed as part of installing the incorrect package.
My goal is to
Debug with Visual Studio's Attach to Process functionality on the local IIS.
It works only when I use the full framework (dnx-clr-win-x64.1.0.0-beta7), but not with the Core CLR (dnx-coreclr-win-x64.1.0.0-beta7).
When I choose the CoreCLR I cannot make any breakpoint, since it says that the symbols are not loaded.
This is my publish.xml:
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
[...]
<CompileSource>True</CompileSource>
<PublishDNXVersion>dnx-coreclr-win-x64.1.0.0-beta7</PublishDNXVersion>
<UsePowerShell>True</UsePowerShell>
<WebRoot>wwwroot</WebRoot>
<WwwRootOut>wwwroot</WwwRootOut>
<IncludeSymbols>True</IncludeSymbols>
[...]
</PropertyGroup>
</Project>
Just created a simple test project from the template (only with
values controller).
I would like to make my next project completely .NET Core based.
That is why I want to make sure everything is working with .NET Core.
I have the full source code of my example.
I am using Visual Studio 2015
With the full CLR it works:
With the core CLR it does not work: