pamidur / aspect-injector

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

Aspect Injector processing has failed #196

Closed Lumoryel closed 1 year ago

Lumoryel commented 2 years ago

Environment (please complete the following information):

Describe the bug When I try to build a project where I define Aspects, sometimes I get this error:

Aspect Injector processing has failed. See other errors. myproject\AspectInjector

Processing failure: System.InvalidOperationException: Expected sequence is not found even once. Unsupported language/version ? myproject\AspectInjector|2.7.4

One time I had to clean to make it work again, one time a new build was enough.

pamidur commented 2 years ago

Hi @Lumoryel , thank you for report, I'll be looking at this issue shortly

pamidur commented 2 years ago

Could you please provide some code samples where issue happens? Most probably the most weird method you applied an aspect to

Lumoryel commented 2 years ago

That was my [Aspected] Method:

public async Task SetPolarizerPosition(double aPolarizerValue) { await internSetPolarizerPosition(aPolarizerValue); await updateAnalyzerPositionIfLinked(); }

Maybe it has something to do with async Methods or Task return values?

Lumoryel commented 2 years ago

That was my [Aspected] Method:

[LogCall]
public async Task SetPolarizerPosition(double aPolarizerValue)
{
    await internSetPolarizerPosition(aPolarizerValue);
    await updateAnalyzerPositionIfLinked();
}

Thats my aspect:

[Aspect(Scope.Global)]
[Injection(typeof(LogCall))]
public class LogCall : Attribute
{
    [Advice(Kind.Before)]
    [DebuggerStepThrough]
    public void LogEnter(
        [Argument(Source.Name)] string aName,
        [Argument(Source.Type)] Type aType,
        [Argument(Source.Arguments)] object[] aArgs,
        [Argument(Source.Instance)] object aInstance
        )
    {
        string aID = getLogIDString(aInstance);

        string aArgsString = "";
        if (aArgs.Any())
        {
            var aArgsStrings = aArgs.Select(createString).ToList();
            aArgsString = string.Join(", ", aArgsStrings);
        }

        string aMessage = $"Invoke {aType.FullName}.{aName}{aID}({aArgsString})";
        Console.WriteLine(aMessage);
    }

    [DebuggerStepThrough]
    private static string createString(object x)
    {
        if (x == null)
        {
            return "<null>";
        }

        var aConverter = System.ComponentModel
            .TypeDescriptor.GetConverter(x.GetType());
        if (aConverter == null)
        {
            return x.ToString();
        }
        return aConverter.ConvertToInvariantString(x);
    }

    private static string getLogIDString(object aInstance)
    {
        string aID = "";
        var aLogID = (aInstance as ILogID)?.GetLogID();
        if (aLogID != null)
        {
            aID = "<" + aLogID.ToString() + ">";
        }

        return aID;
    }

    [DebuggerStepThrough]
    [Advice(Kind.After)]
    public void LogExit(
        [Argument(Source.Name)] string aName,
        [Argument(Source.Type)] Type aType,
        [Argument(Source.ReturnType)] Type aReturnType,
        [Argument(Source.ReturnValue)] object aReturn,
        [Argument(Source.Instance)] object aInstance
        )
    {
        string aID = "";// getLogIDString(aInstance);

        string aReturnString = "";
        if(aReturnType != typeof(void))
        {
            aReturnString = ": " + aReturn?.ToString() ?? "<null>";
        }

        string aMessage = $"Finished {aType.FullName}.{aName}{aID}()" + aReturnString;
        Console.WriteLine(aMessage);
    }
}

public interface ILogID
{
    object GetLogID();
}

I'm using a .NET Framework 4.8 Project with C# 10.0 and #nullable enabled. Maybe that helps?

pamidur commented 1 year ago

Just checking-in. Still working on this one

pamidur commented 1 year ago

Could you please try again with 2.8.1 ? I can't reproduce it with newer versions

Lumoryel commented 1 year ago

So far I can't reproduce the problem. Best Lumo

pamidur commented 1 year ago

Thank you @Lumoryel , feel free to reopen if needed