Visual Studio 16.8 breaks .NET Framework 4.8 WPF build - c#

I've now updated to Visual Studio 16.8, and with that, my existing (large) solution fails to build any WPF projects. Error messages such as the following everywhere:
10>C:\...\src\UserInterface\DataWriterMonitor\App.xaml.cs(5,25,5,28): error CS1558: "App" hat keine passende statische Main-Methode.
10>C:\...\UserInterface\DataWriterMonitor\App.xaml.cs(9,4,9,23): error CS0103: Der Name "InitializeComponent" ist im aktuellen Kontext nicht vorhanden.
Obviously, it's failing to pre-process the xaml files. When I change the csproj header of the offending project from <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop"> I get:
10>C:\Program Files\dotnet\sdk\5.0.100\Sdks\Microsoft.NET.Sdk.WindowsDesktop\targets\Microsoft.WinFX.targets(240,9): error MC1000: Unbekannter Buildfehler, "Could not find assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Either explicitly load this assembly using a method such as LoadFromAssemblyPath() or use a MetadataAssemblyResolver that returns a valid assembly."
This even happens if I remove all direct references from the project.
Different other approaches (including setting the sdk to 4.8 in global.json) generated different error messages, but didn't work either.
How can I make that the build does not use the 5.0 SDK? I need it installed for other projects, but here I just want my solution to build as before with .NET Framework 4.8.
Note: I've tried the sample .NET 4.8 WPF app. This one builds, but it uses the old project file format. If I change it to the new format, I get similar problems. After some fiddling, that now works, possibly because this otherwise empty project has no references at all.

I apparently did too many things at once. Using a global.json with
{
"sdk": {
"version": "3.1.101"
}
}
and no other changes (especially no change to <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">) fixed the build.

I removed <usewpf>true</usewpf> from my projects

Related

"Not a valid framework" (MM0140) error when trying to build Xamarin native bindings library for framework targeting net6-macos

I have working native bindings library targeting old xamarin.mac (xamarin.full net framework 4.8) built with mono.
It is binding ScritptingBridge.framework, not a static library. It is builds and works as expected.
Note: full project for experiments may be found here: https://github.com/snechaev/net6nativeBindingsError
Now I'm trying to migrate to newest net6-macos. To do so I do the following
converted main app project to the sdk style and target net6-macos. If I will temporary remove native bindings dependencies, this will build fine.
converted native bindings project to the sdk style and perform the following fixes suggested by the comiler errors
changed version to avoid wildcards
replaced [assembly: LinkerSafe] with AssemblyMetadata("IsTrimmable", "True")]
added explicit NativeReferecnce to the framework I'm trying to bind.
Final csproj content for native bindings project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6-macos</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RootNamespace>NativeBindingsLib</RootNamespace>
<AssemblyName>NativeBindingsLib</AssemblyName>
<IsPackable>false</IsPackable>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
<IsBindingProject>true</IsBindingProject>
<NoBindingEmbedding>true</NoBindingEmbedding>
</PropertyGroup>
<ItemGroup>
<ObjcBindingApiDefinition Include="ApiDefinition.cs" />
<ObjcBindingCoreSource Include="StructsAndEnums.cs" />
<NativeReference Include="/System/Library/Frameworks/ScriptingBridge.framework" Kind="Framework" />
</ItemGroup>
</Project>
When I trying to build the only bindings library with dotnet build it builds without errors.
But when I try to build the application project (app.csproj in the sample repository), which is referencing (via the the ProjectReference) the binding library, I got the following errors:
ILLINK: Error MM0140: File '/Users/user/work/MonoBindingTest/net6/app/obj/Debug/net6.0-macos/osx-x64/linker-cache/NativeBindingsLib.resources/ScriptingBridge.framework/ScriptingBridge' is not a valid framework. (MM0140) (app)
ILLINK: Error MM2342: The linker step 'Extract Binding Libraries' failed during processing: File '/Users/user/work/MonoBindingTest/net6/app/obj/Debug/net6.0-macos/osx-x64/linker-cache/NativeBindingsLib.resources/ScriptingBridge.framework/ScriptingBridge' is not a valid framework. (MM2342) (app)
I double checked the framework path and it is ok. I also tried to add ScriptingBridge.framework into the XCode project and verified that the path is the same.
I also will be happy to get any samples of working net6-macos targeted native bindings projects for frameworks (not a static libraries) for further experiments on my own.
Full project mey be found here (both mono-targeted and net6-targeted): https://github.com/snechaev/net6nativeBindingsError
Finally, I posted the issue in the xamarin repo on github https://github.com/xamarin/xamarin-macios/issues/15485.
And ducting the discussion and experiments we concluded the following:
MM0140 is because the ScriptingBridge is the system framework and this is the special case.
you should not use NativeReference for the system frameworks as it will try to copy the framework into your bundle and this is not what you want for the system framework
initially I was missleaded by the following error
MSBuild : error MM7068: Can't create a binding resource package unless there are native references in the binding project.
so I decided to add NativeReference pointing ScriptingBridge.
So, the final solution is that the "at least one NativeReference" compiler check is excessive and it will be removed it net7 (https://github.com/xamarin/xamarin-macios/issues/15489).
The temporary workaround for net6 is to add the fake NativeReference to the empty static library to make compiler happy.
<NativeReference Include="/Users/sergey/work/MonoBindingTest/net6/test.dylib" Kind="Static"/>
I believe that it is possible to specify NativeReference to point to the source code (C++) instead of binary file and provide an additional command to build such a C++ code into the binary during the build process of native bindings library.
Additional note: the MM0140 error was called MT0140 in the previous versions of xamarin. So, this note may help other to find this post.

NET core - Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ', .NET 6

I have converted .NET Framework 3.5 TestAPP to .NET 6 TestAPP and worked well.
i have an Automation project(C++ project) which was using .NET Framework 3.5 TestAPP.dll to run the tests, but right now i have replaced the .NET Framework 3.5
.dll with .NET 6 TestAPP i.e. TestAPP.dll
when i ran the exe in command line , i have got an error as
"Unhandled Exception: System.IO.FileLoadException: Could not load file
or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)"
Please let me know if anything needs to be corrected.
Resolved the error by changing the C++/CLI properties from visual studio project properties , which was referring to .NET framework earlier and i have changed the property as .NET core runtime(clr:netcore) and it is internally handled all GAC changes and worked well.
Hope it might help someone .
When upgrading an app to a later .NET version, edit your csproj file and make sure there is no <RuntimeFrameworkVersion> node in there that someone may have inserted manually.
This will not be updated automatically when switching .NET versions
The running project returned this error. When I changed it to the way in the image, the error was solved.
Project>Properties
In my case the error was due to worng Default Project in Package Manager Console, if you are running EF migrations or something that is in a project that need to get info from the API make sure you are targetting the right project, also try clean + build first.

netcoreapp31 vs .net472 conflicts

We have an app which is .netcore 3.1 - a console app.
This uses a 3rd party library from Microsoft which internally creates a dispatcher.
Under an output type of net472 this works, howwever with an output type of netcore31 the following error occurs:
System.TypeLoadException: 'Could not load type 'System.Windows.Threading.Dispatcher' from assembly 'WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.'
For information:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Platforms>AnyCPU;x86</Platforms>
</PropertyGroup>
Does anybody know if there is a way to make this work without having to change to the .net framework which causes a host of other issues mainly related to receiving serialized messages from other apps which have a serialization scheme originating in .net core 3.1.
I had been receiving the same error message, but for my .net core 5.0 testing project. After attempting several other suggestions I ran across this post and used the commented answers to "add true" as well as adding net5.0-windows.
This may not completely map to your use case as you are using a library that should be able to create your dispatcher, but for my dispatcher error this worked.

Error: Could not load file or assembly 'Microsoft.Win32Registry' from .NET Standard Library Consumed by .NET Framework Console App

The Problem
The Microsoft.Win32.Registry nuget package is giving me a lot of issues . We had .NET Framework 4.7.2 library that had a helper class that used that package and worked fine from a .NET Framework 4.7.2 unit test project. We recently converted the library to target .NET Standard 2.0, and now this class breaks when used with the following error:
ERROR: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Win32.Registry, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.Win32.Registry, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at NetStandardRegistryLib.RegistryUtil..ctor(String remoteMachineName)
at NetStandardRegistryLib.Program.Main(String[] args) in C:\src\Microsoft.Win32.Registry-IssueRepro\FrameworkRegistry\RegistryEditor\Program.cs:line 21
When I look at output of the Console project, I don't see the Microsoft.Win32.Registry.dll in the output.
Using ILSpy and loading my .exe - it seems it would try to resolve the .dll from my dotnet core installation, which wouldn't work since it's a .NET Framework app.
To Reproduce
I've created a repo that reproduces for me on my Windows 2010 machine. You can find it here: https://github.com/TylerNielsen/Microsoft.Win32.Registry-IssueRepro
Clone the repo, then open and build the RegistryEditor project.
Using Powershell or your favorite editor - call the tool with three required arguments [remoteMachineName] [Environment variable name to set] [Value to set on the environment variable] **
** Note, this actually requires a remote machine you can reference by IP address. For some reason using 127.0.0.1 doesn't work for me, but I'm unsure why. The use case for this utility is for accessing remote machine environment variables.
Other Notes
When I create a .NET Framework Class Library to use for unit tests and reference the same .NET Standard Library - the Microsoft.Win32.Registry package is included and the utility runs just fine.
In your repo, if you look into both csproj files you'll see they are quite different. The NetStandardRegistryLib.csproj is the new format, the RegistryEditor.csproj is the old one.
Your problem is that old style projects do not resolve dependencies transitively. RegistryEditor will not copy dependencies of its own dependency NetStandardRegistryLib.
There are at least two ways to solve your problem:
Just add Microsoft.Win32.Registry Nuget package to RegistryEditor project explicitly. That'll make the Microsoft.Win32.Registry.dll to appear in the RegistryEditor build folder.
Convert the RegistryEditor.csproj to new format. It's not limited to netstandard builds, you can use it for builds targeting .Net Framework versions as well. And it does resolve dependencies transitively!
Here's a long guide for how to do the conversion in general https://natemcmaster.com/blog/2017/03/09/vs2015-to-vs2017-upgrade/
Since your project is quite simple, I've converted it for you. Just replace the content of RegistryEditor.csproj with the code below. It's still targeting net472.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AssemblyName>RegistryEditor</AssemblyName>
<RootNamespace>RegistryEditor</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\NetStandardRegistryLib\NetStandardRegistryLib.csproj" />
</ItemGroup>
</Project>
Note that several properties that used to be in AssemblyInfo.cs file are provided by csproj file properties now, so you need to remove them from AssemblyInfo.cs to fix compilation errors:
[assembly: AssemblyTitle("RegistryEditor")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RegistryEditor")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Mvvm-light / Serilog design time error: Could not load file or assembly [VS2017]

I'm getting the error below in DesignTime when I have a view open which has a viewmodel with references to SeriLog.
Could not load file or assembly 'Serilog, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=24c2f752a8e58a10' or one of its
dependencies. Het systeem kan het opgegeven bestand niet vinden. myapp
C:\snip\Projects\snip\myapp\myapp\Views\MyViews\MyView.xaml
The dutch string translated to English: "The system can't find the file"
The project compiles and runs fine, but I'd like an error-free design-time experience. Right now this error breaks my design-time project code in VS2017.
What can I do to fix this error?
Already tried:
Removing the serilog folder from %userprofile%.nuget and rebuilding
Cleaning the project folder bin\ and obj\ folder
Rebuilding the project
Restarting VS2017
Rebooting computer
Another workstation shows the same error
Compiling serilog and sink myself on netcore1.6 target
I'm building an UWP app targeting the anniversary edition 14393 framework.
project.json serilog references:
"Serilog": "2.4.0",
"Serilog.Sinks.RollingFile": "3.3.0",
Sample VS project which replicates the issue
I have created a blank Template10 project with mvvm-light viewmodel on DetailPage.xaml for which the serilog error is shown.
Download location:
https://www.dropbox.com/s/uocjx0kuc1dknwt/Template10_Serilog_Sample.zip?dl=0
Screenshot of error
I think you are using higher .net framework version.
I also faced same problem earlier in asp.net mvc project in vs2017, but my problem is resolved when i changed my target .net framework version to lower one.
Try to use the Package Manager to update.
Go to: Tools > Nuget Package Manager > Package Manager Console
And type it:
Update-Package Serilog

Categories