Unhandled exception (PostSharp.Compiler.Hosting.CommandLine.dll 6.0.24 release | .NET Core 2.1.2): System.NullReferenceException: Object reference not set to an instance of an object.

perodiaro's Avatar

perodiaro

04 Aug, 2018 06:45 PM

Got an error at solution build.

Build log:

1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: Unhandled exception (PostSharp.Compiler.Hosting.CommandLine.dll 6.0.24 release | .NET Core 2.1.2): System.NullReferenceException: Object reference not set to an instance of an object.
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.AspectInfrastructure.TypeInitializationManager.^nCElfVTrtpDd.^Dpi1V+WW(MethodDefDeclaration _0) in ^xo25+YorCGvs:line 630
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.AspectInfrastructure.TypeInitializationManager.^nCElfVTrtpDd.^WKeyjjZv(TypeDefDeclaration _0, ^IfscAxyN _1, MethodDefDeclaration _2, ^n0mSh9Dz _3) in ^xo25+YorCGvs:line 605
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.AspectInfrastructure.TypeInitializationManager.^uPjZnkQNGku3.^r4DISfQ8(TypeDefDeclaration _0, ^IfscAxyN& _1) in ^xo25+YorCGvs:line 542
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.AspectInfrastructure.TypeInitializationManager.^r4DISfQ8() in ^xo25+YorCGvs:line 959
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at ^YRTd8AcBbva/.Execute() in ^HpM7ehxuEZEb:line 29
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.Extensibility.Project.ExecutePhase(String phase) in ^wJ3oIHJDddm/:line 2321
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Sdk.Extensibility.Project.Execute() in ^wJ3oIHJDddm/:line 2388
1>C:\Users\Admin\source\repos\TestDllLoad\TestDllLoad\TestDllLoad.csproj : error PS0099: at PostSharp.Hosting.PostSharpObject.InvokeProject(ProjectInvocation projectInvocation) in D:\src\PostSharp-5\Core\PostSharp.Hosting\PostSharpObject.cs:line 283.
1>Done building project "TestDllLoad.csproj" -- FAILED.

Source code:

[FinalAspect]
    public interface ITest
    {
    }

    public interface ITestImpl
    {
        bool IsLoaded { get; set; }
    }

    [IntroduceInterface(typeof(ITestImpl), OverrideAction = InterfaceOverrideAction.Ignore)]
    [PSerializable]
    public class TestAspect : InstanceLevelAspect, ITestImpl
    {
        public bool IsLoaded { get; set; } = new Random().Next(0, 100) > 50;
    }

    [PSerializable]
    [MulticastAttributeUsage(Inheritance = MulticastInheritance.Multicast)]
    public class FinalAspect : TypeLevelAspect, IAspectProvider
    {
        public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
        {
            Type targetType = (Type)targetElement;

            if (!HasBaseAspect(targetType) && targetType.GetInterfaces().All(x => x != typeof(ITestImpl)))
            {
                yield return new AspectInstance(targetType, new TestAspect());
            }
        }

        private bool HasBaseAspect(Type targetType)
        {
            IAspectRepositoryService repository = PostSharpEnvironment.CurrentProject.GetService<IAspectRepositoryService>();
            return repository.HasAspect(targetType, typeof(TestAspect)) ||
                   (targetType.BaseType != null && HasBaseAspect(targetType.BaseType));
        }
    }

    public class Test1 : ITest { }
    public class Test2 : ITest { }
    public class Test3 : ITest { }
    public class Test4 : ITest { }
    public class Test5 : ITest { }
    public class Test6 : ITest { }
    public class Test7 : ITest { }
    public class Test8 : ITest { }
    public class Test9 : ITest { }
    public class Test10 : ITest { }
  1. Support Staff 1 Posted by PostSharp Techn... on 06 Aug, 2018 01:54 PM

    PostSharp Technologies's Avatar

    Hello,

    we have successfully reproduced the bug and it seems to be quite old (the same behavior can be observed in PostSharp 4.3). We will be working on a fix.

    Thanks for reporting it.

    (internal issue #16490)

    All the best,
    Daniel

  2. Support Staff 2 Posted by PostSharp Techn... on 21 Aug, 2018 03:52 PM

    PostSharp Technologies's Avatar

    Hello,

    We're closing the ticket for now as the bug has been internally filed as issue #16490. We will contact you as soon as the bug fix has been released.

    For more details on our support policies and prioritization of bug fixes, please visit https://www.postsharp.net/support/policies

    Thanks,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 21 Aug, 2018 03:52 PM.

  4. PostSharp Technologies re-opened this discussion on 11 Sep, 2018 09:07 AM

  5. Support Staff 3 Posted by PostSharp Techn... on 11 Sep, 2018 09:07 AM

    PostSharp Technologies's Avatar

    Hello,

    I would like to do a slight update - we have found that the exception in the reproduction was caused by IAspectProvider providing an InstanceLevelAspect to the interface. This is not valid use case and we are going to add an error message for this case. I just want to make sure that this is what you experienced.

    If you change the implementation of ProvideAspects in the reproduction to the following the exception disappears:

    public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
    {
        Type targetType = (Type)targetElement;
    
        if (targetType != typeof(ITestImpl) && !HasBaseAspect(targetType) && targetType.GetInterfaces().All(x => x != typeof(ITestImpl)))
        {
             yield return new AspectInstance(targetType, new TestAspect())
        }
    }
    

    Does this also solve what you have originally experienced?

    Best regards,
    Daniel

  6. 4 Posted by perodiaro on 14 Sep, 2018 03:13 PM

    perodiaro's Avatar

    With code you provided i got same exception. Attached screenshot.

  7. 5 Posted by perodiaro on 14 Sep, 2018 03:22 PM

    perodiaro's Avatar

    changed targetType != typeof(ITestImpl) to targetType != typeof(ITest) and it works fine.

  8. Support Staff 6 Posted by PostSharp Techn... on 17 Sep, 2018 02:24 PM

    PostSharp Technologies's Avatar

    Hello,

    the bug #16490 has been fixed in the current release of PostSharp 4.3.45, 6.0.27 and 6.1.3-preview.

    Should you need further help with this issue, don't hesitate to re-open this discussion.

    Best regards,
    -tony

  9. PostSharp Technologies closed this discussion on 17 Sep, 2018 02:24 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac