Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a Fody MethodDecorator in another project?

I'm trying to use the Fody MethodDecorator to log method entry/exit in multiple projects. I've created a project in visual studio and implemented the IMethodDecorator interface as described in the ReadMe.md in the Fody MethodDecorator project.

The Fody MethodDecorator works fine in the one project where I've implemented the IMethodDecorator interface and I can log method entries/exits. But when I try to log method entry/exits in other projects by adding a reference to my first project, there is no logging in any other projects.

I've created a github repository with a minimal working example just to show the issue, click here for link.

There are others that have had the same issue, like this post from 3 yeas ago. But the solution in that post is to add the Fody and MethodDecorator nuget packages to all projects. I have already added both nuget packages to all projects. I have also added the FodyWeavers.xml to all projects.

If you want to know what my code looks like, just check out the git repo. Is there anything else I have to do to get logging of method entry/exit working in other projects?

I would like to simply add a reference to my first project and reuse the method decorator from the first project. I'm using .Net 4.8, Fody 6.5.3 and MethodDecorator.Fody 1.1.1.

Edit If you download the repository and get the error: A numeric comparison was attempted on "$(MsBuildMajorVersion)" that evaluates ... This is apparantly due to a bug in Visual Studio, that can be overcome by a restart.

like image 337
FluffyBike Avatar asked Nov 21 '25 18:11

FluffyBike


1 Answers

For Fody.MethodDecorator to work in other projects, you need to add the module [module: Interceptor] in each project.

My suggestion is to create an Interface as below in each project, and make sure your classes inherit from the interface. ( This way you only added the module at one place in the project)

[module: Interceptor]


public interface IInterceptor
{
    // no methods here.
    // the interface is used only to register the Interceptor attribute at the module level.
}

public class SomeClass : IInterceptor
{
    [Interceptor]
    public void SomeMethod()
    {
    }
}
like image 99
Vishesh Singh Avatar answered Nov 23 '25 08:11

Vishesh Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!