Can Fody and its plugins be used with .NET Core (3.0)? - wpf

As the title says, I'm having trouble getting Fody, and the plugin Fody.PropertyChanged, to work in .NET Core 3.0, or any .NET Core version. Reading the issues on the respective GitHub pages doesn't answer my question, nor am I able to find any relevant answers.
Once installed I cannot run my WPF project anymore and I am given the following error:
The target process exited without raising a CoreCLR started event.
Ensure that the target process is configured to use .NET Core.
This may be expected if the target process did not run on .NET Core.
The program '[21820] CalculationToolsApp.exe' has exited with code -2147450749 (0x80008083).
Any suggestions?
EDIT: I found out that I (maybe) cant use "Fody.Costura" with "Fody.PropertyChanged" like this in the FodyWeavers.xml file:
<Weavers>
<PropertyChanged />
<Costura />
</Weavers>
Which shouldn't be a problem because with .NET Core I can create a single file application anyway. Removing the Costura reference from the FodyWeavers.xml solved my problem!

It should work. Fody is compatible with .NET Standard.
Create a new WPF app using the WPF App (.NET Core) template in Visual Studio 2019 or using the dotnet new wpf command
Install the Fody and PropertyChanged.Fody NuGet packages
Add a file named "FodyWeavers.xml" with the following contents to the project:
<Weavers>
<PropertyChanged />
</Weavers>
Build
If you then decompile the assembly using a decompiler such as for example dotPeek, you should see the injected code as expected, e.g.:
public string GivenNames
{
// Method get_GivenNames with token 06000009
get
{
return this.<GivenNames>k__BackingField;
}
// Method set_GivenNames with token 0600000A
set
{
if (string.Equals(this.<GivenNames>k__BackingField, value, StringComparison.Ordinal))
return;
this.<GivenNames>k__BackingField = value;
this.<>OnPropertyChanged(<>PropertyChangedEventArgs.FullName);
this.<>OnPropertyChanged(<>PropertyChangedEventArgs.GivenNames);
}
}

Costura didnt work in wpf with .net core 3.1 for me either.
In .net core 3.1 you can use this instead:
Build -> publish -> create profile -> Edit "Configuration"
Target Runtime = win-x64 (or what ever target system you want, but NOT "portable")
expand "File Publish Options"
check: Produce single file
save
When you now choose build -> publish -> publish button it will create the single file.
It seems to be that they stopped the costura project because of the "Single-file executables" feature of .net core. Though this feature is still behind costura because you have to set a target runtime.
https://github.com/Fody/Costura/issues/442
In dotnet core 3 there are two new features
Single-file executables
Assembly linking
With these features included in the dotnet tool set, the value
proposition of Costura is greatly diminished. With that in mind I
think long term Costura should cease to be used as people transition
over.
Please comment with any input.
Plan:
disable issues
PR will still be accepted but only for bug fixes
add note to readme
add note to nuget description
write a warning in
update for .NET 5:
for .NET 5 and the current visual studio version 16.10.2 the wizard changed. I could not get this to work with the wizard anymore though i checked the options for single file etc.. But using the console worked: tools -> command line -> developer command prompt -> enter this:
dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true
.NET 5 not compiling to single file executables

Related

MSB4062 error publishing .NET7 app with publish profile

This is pretty easy to replicate but also code here:
https://github.com/dominicshaw/dotnet-publish-error
create an empty wpf project using .NET7
create a publish profile for ClickOnce
publish via command line (not visual studio)
Publish profile is almost totally standard (view here)
Command line from project directory:
dotnet publish PublishError.csproj -p:PublishProfile=ClickOnceProfile
Subsequent error:
MSBuild version 17.4.0+18d5aef85 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.100\Microsoft.Common.CurrentVersion.targets(4149,5): error MSB4062: The "Microsoft.Build.Tasks.RequiresFr
amework35SP1Assembly" task could not be loaded from the assembly Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKey
Token=b03f5f7f11d50a3a. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, a
nd that the task contains a public class that implements Microsoft.Build.Framework.ITask. [C:\Users\shawd\source\repos\PublishError\Publi
shError\PublishError.csproj]
I have the latest SDKs installed and VS2022 up to date. This only happens via command line - I am able to publish from Visual Studio (I am setting up devops, so require command line).
This happens regardless of adding nuget package for Microsoft.Build.Tasks.Core. Note however nuget package v15.1.0.0 is not available - I have tried 15.1.548 and the latest instead.
Totally at a loss! Any ideas very welcome.
Thanks vm
During the tests with your project and files, I finally managed to publish it successfully with the command dotnet publish PublishError.csproj -p:PublishProfile=FolderProfile.
Here is the document for dotnet publish, as is referred,
The preceding example uses the FolderProfile.pubxml file that is found
in the <project_folder>/Properties/PublishProfiles folder. If you
specify a path and file extension when setting the PublishProfile
property, they are ignored. MSBuild by default looks in the
Properties/PublishProfiles folder and assumes the pubxml file
extension.
And I also succeeded with dotnet publish with your project.
==============================
update on 11/19
So I suppose that the issue is resulted from your definition for -p:PublishProfile=ClickOnceProfile,it will change the path where this command would search for the publish file
A partial answer as my workaround for anyone struggling with a similar problem:
I have found it impossible to do this with the dotnet cli but I can do it with MSBuild if I specifically target 17.4 (VS2022). By default, my Azure DevOps Server 2020 capability is the 2019 MSBuild (15.X), so I have to be explicit:
"C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Current\Bin\MSBuild.exe" /restore /Verbosity:m /t:Publish /p:RuntimeIdentifier=win-x64 /p:configuration=Release /p:PublishProfile=ClickOnceProfile
This works - so in my pipeline I have created a batch file which runs this, then I copy the files to my ClickOnce location.
Not ideal, but a working pipeline.
Interesting side note - this dotnet cli command which should be idenitical (and indeed uses the correct 17.4 MSBuild) - does not work (with the same error):
dotnet msbuild -target:Publish -property:RuntimeIdentifiers=win-x64;Configuration=Release;PublishProfile=ClickOnceProfile

Microsoft.Bcl.AsyncInterfaces error (in CSVHelper method) when running the application installed by Visual Studio Setup project

I have a WPF application that reads a CSV file using CSVHelper which I installed using NuGet. Here's the project, system, etc version info.
Microsoft Windows 7 Professional Version 6.1.7601 Service Pack 1 Build
7601
Microsoft Visual Studio Community 2019 Version 16.4.5
NuGet Package Manager 5.4.0
WPF Project Target Framework: .NET Framework 4.7.2
CSVHelper Version: 15.0.5
CSV reader method is straightforward; read a file and put it in a list. Nothing fancy. I'll link the entire project at the bottom so you can download and try it out if you want.
When I run the executable made in either Release or Debug builds, they work fine. They read the file, put contents in a list, and display on a DataGrid just fine. As a next step, I created a standard Visual Studio Setup project, and created an msi installer, which I used to install the app in my PC.
When I run that executable (which is in my Program Files directory), however, the application throws an exception. Here's the exception message and Stack Trace.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
at ReadCSVTest.MainWindow.ReadPeopleList(String path, List`1& people, String& msg)
at ReadCSVTest.MainWindow..ctor()
Since the error says couldn't load the assembly Microsoft.Bcl.AsyncInterfaces, I added that from NuGet and ran everything again, and I still get the error. What's the problem here? What does it mean that Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context.? And why do I only get this exception when I run the executable installed by the Setup project?
Please download the entire project from HERE.
Just so anyone runs into the same issue in the future; as Jones suggested in the comments, downgrading each of the two libraries to following versions fixed the issue for me.
CsvHelper ==> 12.3.2
And
Microsoft.Bcl.AsyncInterfaces ==> 1.0.0

Create a WinForm project based on .net core 3

I am trying to figure out what I am missing here, must be something quite obvious but I am unable to see it.
what I am trying to achieve is to have a working solution/project of WinForms app based on .net core 3. it might be too early to look into it as they just announced it but still why not if we can. here are steps I've done so far:
create a blank solution
run the ".net new winforms" command
attempt to add the generated project to the solution.
i'm getting the error: Project File is incomplete. Expected imports are missing.
here is the output of the CLI:
Package Manager Console Host Version 4.9.2.5706
Type 'get-help NuGet' to see all available NuGet commands.
PM> dotnet --list-sdks
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.2.105 [C:\Program Files\dotnet\sdk]
3.0.100-preview3-010431 [C:\Program Files\dotnet\sdk]
PM> dotnet new winforms
The template "Windows Forms Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on C:\DevProjects\winformscore3\winformscore3.csproj...
Persisting no-op dg to C:\DevProjects\winformscore3\obj\winformscore3.csproj.nuget.dgspec.json
Restore completed in 113.9 ms for C:\DevProjects\winformscore3\winformscore3.csproj.
Restore succeeded.
PM>
https://dotnet.microsoft.com/download/dotnet-core/3.0
"To use .NET Core 3.0 with Visual Studio, you'll need Visual Studio 2019 Preview."

how to Step through Catel code while debugging ?

After adding Catel implementation to one of my views(mainPage) in my project I have a bug that crashes my Silverlight project. The only indication I have is :" A first chance exception of type 'System.NullReferenceException' occurred in Catel.MVVM " (just about after setting the RootVisual~) - I can't find what I'm doing wrong, and since I am planing on a long-term relationship with Catel I thought that stepping through Catel code might come in handy, so I tried and failed in all the following steps (any help will be appreciated.. because currently I'm stuck and can't find anything):
downloaded catel 3.9 source files (same as my nuget package) and tried building it.. failed with this error :
E:\Dev\Catel-3.9.0\src\Catel.Core\Catel.Core.SL5\Fody.targets(51,5): error MSB4036: The "Fody.WeavingTask" 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 in the project file, or in the *.tasks files located in the "E:\WINDOWS\Microsoft.NET\Framework\v4.0.30319" directory. - trying to search and figure the reasons and overcome this, lead me to nowhere. :(
i have followed this "Stepping through the code" instruction in Catel documentation but noting really happened (I'm using VS2010..is this an issue ?? )
I read this article & readme.md at : https://github.com/GeertvanHorrik/GitHubLink about using GitHubLink to help you stepping through Catel code while debugging. I downloaded the release GitHubLink 1.3.0 from https://github.com/GeertvanHorrik/GitHubLink/releases/tag/1.3.0 ... but running this even with just the githublink.exe -help flag had thrown a exception..
I downloaded the Githublink-master source files but 0 projects were loaded (the GithubLink project file is incompatible with the current version of VS - I'm using VS2010)
can anyone help me either shade some light or:
overcome the build error I receive for catel (no.1)
instruct me how to set & step into catel ?
share the pdb files for Catel libs
It completely depends on the version you are using. If you are using the latest official one (3.9), stepping through the code is only possible by cloning the master branch and building the PDB files yourself.
In the latest prerelease versions via NuGet (the upcoming 4.0 version) you only have to enable the source server checkbox.
Note that you should never have to use GitHubLink yourself, that is for developers only (we run it during the build of Catel).
About the Fody task: it looks like the NuGet packages have not yet been restored on your side. In the lib folder there is a RestorePackages.bat which you can run to restore the packages.
I tried debugging on VS2012 and using Catel 4 pre-release (from Nuget)..
although I believe I did all the right things & settings in VS, I didnt get much further..
So I Opened (using a bin editor) the pdb file that was pulled by Nuget with the DLL, and took a look at the bin file.
the file have strings pointing to the source files at this directory :
C:\ci_ws\WS\1629\source\catel\src\catel/mvvm\catel.mvvm.shared..
So I have created a tree that starts with
c:\ci_ws\ws\1629\source
and downloaded the catel folder to that dir, renamed it from
"Catel-Develop" to just "Catel" in order to feet the location the pdb pointed.
To get things going I also had to add a "Symbol File(.pdb) location" in VS
option-> settings->Debugging->Symbols..(location/server list) to where the nuget download the packages.
in my case.. SolutionFolder\Packages\Catel.MVVM.3.9.0.1406062245-beta\lib\sl50..(I'm using Silverlight)
and then I was able to load the symbols and step/break into the code successfully ..
Maybe I missing something and there is a better way, but this the only way that worked for me..
The only question that still remains for me now is : What is the name of the Catel Github source branch that is matching the pre-release dll and pdb that Nuget is publishing at the moment (3.9.0.1406062245) ?
(I unchecked the general debug settings of : require source files to exactly match the original version)

How do I use Microsoft.Build.BuildEngine to Build a Silverlight project?

I am using the Microsoft.Build.BuildEngine to build a number of projects. Projects that do not use Silverlight are building correctly while projects that do use Silverlight are not building. All of these projects (Silverlight and non-Silverlight) all build fine through Visual Studio 2008. I get the error message:
error CS0234: The type or namespace name 'SilverlightControls' does not exist in the
namespace 'System.Web.UI' (are you missing an assembly reference?)
I've added System.Web.Silverlight to the project that is doing the build using Microsoft.Build.BuildEngine. I am able to see that namespace in the project doing the build also (so it appears that the project doing the build does have access to System.Web.UI).
Any ideas on how I can avoid that error?
Turned up the verbosity on the build through VS2008 and compared to the log created from the Microsoft.Build.BuildEngine. It looks like the Microsoft.Build.BuildEngine is producing lines like:
Considered "c:\Program Files\Reference Assemblies\Microsoft\Framework
\v3.5\System.Web.Silverlight.dll", but it didn't exist.
It doesn't exist in that location, should it? There are actually a number of locations that it looks for that dll and doesn't find it. I added the reference by right clicking, add reference, .Net, add. There doesn't appear to be a System.Web.Silverlight.dll on my machine (which is odd because the project does compile using VS2008).
Comparing the possibly relevant sections of the two logs:
Microsoft.Build.BuildEngine
For SearchPath "{GAC}".
Considered "System.Web.Silverlight, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL", which was not found in
the GAC.
Visual Studio 2008
System.Web.Silverlight, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL
Manually added System.Web.Silverlight to the path that the Microsoft.Build.BuildEngine was looking for it (coppied it from the output directory when I build the project via VS2008). This solved that problem, but I now get the following errors on the Silverlight components of the solution:
C:\Program Files\MSBuild\Microsoft\Silverlight
\v2.0\Microsoft.Silverlight.Common.targets(95,9): error : The Silverlight 2 SDK is not
installed.
Any ideas?
In the application that is running the Microsoft.Build.BuildEngine I changed the type of build from Any CPU to x86. This has now made the application be able to build the target as expected.
So you're programmatically building using the MSBuild APIs?
It may be useful to add an ILogger (e.g. that just prints to the console) to engine.Loggers, and compare the output with the output window in VS (after setting e.g. 'tools\options\project & solutions\build & run\msbuild output verbosity' to verbose)... seems like somehow the environment is causing assemblies to resolve differently?
Set build type of project to Any. This helps. Happy coding...

Resources