.netfody

How to setup PropertyChanging.Fody without errors?


I've been using PropertyChanged.Fody for years.

Now I want to also intercept PropertyChanging (to disallow certain property changes). So I installed PropertyChanging.Fody, but it caused errors. So I uninstalled and deleted anything related to Fody in my projects, and reinstalled only PropertyChanging.Fody. Still I get an error.

I'm not even using or implementing anything Fody in my project currently. I just installed and already I can't compile. Here is the error I get:

1>MSBUILD : warning FodyPackageReference: Fody: The package reference for PropertyChanging.Fody does not contain PrivateAssets='All' 1>MSBUILD : error : Fody: An unhandled exception occurred: 1>MSBUILD : error : Exception: 1>MSBUILD : error : Failed to execute weaver C:\Users\y2.nuget\packages\propertychanging.fody\1.30.3\build..\weaver\PropertyChanging.Fody.dll 1>MSBUILD : error : Type: 1>MSBUILD : error : System.Exception 1>MSBUILD : error : StackTrace: 1>MSBUILD : error : at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 222 1>MSBUILD : error : at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 113 1>MSBUILD : error : Source: 1>MSBUILD : error : FodyIsolated 1>MSBUILD : error : TargetSite: 1>MSBUILD : error : Void ExecuteWeavers() 1>MSBUILD : error : Object reference not set to an instance of an object. 1>MSBUILD : error : Type: 1>MSBUILD : error : System.NullReferenceException 1>MSBUILD : error : StackTrace: 1>MSBUILD : error : at ModuleWeaver.HasPropertyChangingEvent(TypeDefinition typeDefinition) 1>MSBUILD : error : at ModuleWeaver.HierarchyImplementsINotify(TypeReference typeReference) 1>MSBUILD : error : at ModuleWeaver.HierarchyImplementsINotify(TypeReference typeReference) 1>MSBUILD : error : at ModuleWeaver.PopulateINotifyNodes(List`1 typeNodes) 1>MSBUILD : error : at ModuleWeaver.BuildTypeNodes() 1>MSBUILD : error : at ModuleWeaver.Execute() 1>MSBUILD : error :
at InnerWeaver.ExecuteWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 186 1>MSBUILD : error : Source: 1>MSBUILD : error : PropertyChanging.Fody 1>MSBUILD : error : TargetSite: 1>MSBUILD : error : Boolean HasPropertyChangingEvent(Mono.Cecil.TypeDefinition) 1>MSBUILD : error : 1>Done building project "General.vbproj" -- FAILED.

I have 2 Projects. a vase project called "General". and another Project called MyPhone which references General. I installed to both projects. and both give the same error.

The weavers were created in both projects and seem okay.

Deleting bin/obj and cleaning solution multiple times makes no difference.

What can the issue be?

In the end of course I need both. Also PropertyChanging and aslo PropertyChanged.

I'd appreciate any help

Thanks in Advance

Additional Info

This is my regular weavers I've always used:

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<PropertyChanged CheckForEquality="false"/>
</Weavers>

If I just add <PropertyChanging />, then I get an error:

Fody: No weavers found for the configuration entries PropertyChanging. Add the desired weavers via their nuget package.

And if I go ahead and install ProperrtyChanging.Fody, Then I get again the above long error about ExecuteWeavers.


Solution

  • tl;dr - add the following to the vbproj file via a text editor:

    <Reference Include="Microsoft.VisualBasic" />
    

    EDIT: since the first version of this answer, this is now included in the official wiki of the repo.


    Details:

    After some research, this seems to be a bug / undocumented requirement when using PropertyChanging.Fody with a VB.NET project (but not C#) on .NET Framework (tested with 4.8). It doesn't happen with .NET Core / .NET5+.

    It's related to the issue of the project not providing all the required references to MSBuild in a way that Fody can use: https://github.com/Fody/PropertyChanged/issues/313

    No actual code is required to reproduce it, a fresh vb project based on .NET Framework 4.8 with added PropertyChanging.Fody NuGet package will not compile, regardless of the version of the nuget package or of Fody itself (tested with all versions Fody 1.x.x - 6.x.x). Same with a c# project.

    I'll report the issue to the developers. In the meantime, the workaround is to manually add the missing requirements. In my case, and for a VB project, this included first the following two to even get to your error:

    <Reference Include="System" />
    <Reference Include="mscorlib" />
    

    And then this one to solve it:

    <Reference Include="Microsoft.VisualBasic" />