pamidur / aspect-injector

AOP framework for .NET (c#, vb, etc)
Apache License 2.0
770 stars 115 forks source link

Build fails when creating aspects in VB.NET but not in C# #173

Closed Meigs2 closed 2 years ago

Meigs2 commented 2 years ago

I'm researching using aspect-injector in a large vb.net project, however attributes created in VB will not compile, but will in C# of the same project type.

Steps to reproduce:

  1. Create 2 .Net Framework 4.7.2 console apps
  2. Add ApsectInjector package to both
  3. Add the following classes to the respective projects:

C#:

    [Aspect(Scope.Global)]
    [Injection(typeof(TraceAspectAttribute))]
    public sealed class TraceAspectAttribute: Attribute
    {
        [Advice(Kind.Before, Targets = Target.Method)]
        public void TraceStart(
            [Argument(Source.Type)] Type type,
            [Argument(Source.Name)] string name)
        {
            Console.WriteLine($"[{DateTime.UtcNow}] Method {type.Name}.{name} started");
        }
    }

VB.NET:

    <Aspect(Scope.[Global])>
    <Injection(GetType(TraceAspectAttribute))>
    Public NotInheritable Class TraceAspectAttribute
        Inherits Attribute

        <Advice(Kind.Before, Targets:=Target.Method)>
        Public Sub TraceStart(
        <Argument(Source.Type)> ByVal type As Type,
        <Argument(Source.Name)> ByVal name As String)
            Console.WriteLine($"[{DateTime.UtcNow}] Method {type.Name}.{name} started")
        End Sub
    End Class

The C# project will compile, but the VB.NET one will not, with the following output:

1>AspectInjector|2.6.1 : error AI_ERR0: Processing failure: System.NullReferenceException: Object reference not set to an instance of an object.
1>     at AspectInjector.Core.Services.InjectionReader.FindInjections(TypeDefinition type)
1>     at AspectInjector.Core.Services.InjectionReader.<>c.<ExtractInjections>b__4_0(CustomAttribute a)
1>     at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
1>     at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray()
1>     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
1>     at AspectInjector.Core.Services.InjectionReader.ExtractInjections(ICustomAttributeProvider target)
1>     at AspectInjector.Core.Services.InjectionReader.ReadAll(AssemblyDefinition assembly)
1>     at AspectInjector.Core.Processor.PatchAssembly(AssemblyDefinition assembly, Boolean optimize, Boolean verbose)
1>     at FluentIL.PatcherBase.Process(String assemblyFile, IAssemblyResolver resolver, Boolean optimize, Boolean verbose)
1>     at AspectInjector.Compiler.Execute(String filename, IReadOnlyList`1 references, Boolean optimize, Boolean verbose). Please submit an issue to https://github.com/pamidur/aspect-injector
1>AspectInjector : error AI_FAIL: Aspect Injector processing has failed. See other errors.
pamidur commented 2 years ago

Hi, @Meigs2 , thank you for the report, I'll take a look asap

Meigs2 commented 2 years ago

I've tried to debug this myself however I'm having issues getting to the seemingly suspect block of code with my VS setup, and the debugger launching many many times every time I build is rather annoying. Is there a way to speed this up/disable? Despite this, InjectionReader.FindInjections doesn't seem to throw anywhere when I debug and try to build a test project in VB.

pamidur commented 2 years ago

That would help a lot if you could debug it. I still cannot find time to write proper doc on how to, but in short:

  1. Clone this repo
  2. init submodule fluent-il
  3. Right click on ApsectInjector project -> build (in DEBUG). Just this project so no test projects debug pop up appears
  4. Leave this VS window open
  5. Open your solution in other VS instance
  6. Modify (add to) your csproj/vbproj like
    <PropertyGroup>
    <_ASI_Location>path to folder where debug version of aspectinjector.dll is located with trailing slash e.g. d:\ai\scr\AspectInjector\bin\netcoreapp2.1\Debug\</_ASI_Location>
    <_ASI_Debug>true</_ASI_Debug>
    </PropertyGroup>
  7. Hit F5 in your solution (second instance of VS)
  8. Debug pop will appear - select your first instance of VS with aspectinjector solution
  9. Optionally set <_ASI_Debug>false for test projects in AspectInjector solution so that they do not annoy you
Meigs2 commented 2 years ago

I've finally gotten around to attempting to debug these issues, however it seems that I cant get attributes to attach when referencing the AspectInjector broker project, but will attach when using the nuget package. Do I need to target a dll directly to get AspectInjector to add aspects?

pamidur commented 2 years ago

fixed in #2.7.3-pre1