Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object reference not set to an instance of an object When calling a function with Attribute From Test Project #210

Open
Krummris opened this issue Sep 11, 2024 · 3 comments

Comments

@Krummris
Copy link

Krummris commented Sep 11, 2024

I have a package that execute some code when i add the Attribute to a function, it work normally when i execute the project normally, but in the tests i am getting the error:

Outcome: Failed
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at MyProject.DataAnnotations.ExecuteBeforeAttribute.Advise(MethodAsyncAdviceContext context)
at ArxOne.MrAdvice.Utility.ExceptionExtensions.Rethrow(Exception exception)
at ArxOne.MrAdvice.Invocation.GetResult(Task advisedTask, AdviceValues adviceValues)
at ArxOne.MrAdvice.Invocation.<>c__DisplayClass4_0.b__0(Task t)
at ArxOne.MrAdvice.Threading.TaskContinuer1.<>c__DisplayClass0_01.b__0(Task t)
at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)

My class in the package

using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using ArxOne.MrAdvice.Advice;
using Microsoft.Extensions.Logging;

namespace MyProject.DataAnnotations;

[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class ExecuteBeforeAttribute(string? description = null) : Attribute, IMethodAsyncAdvice
{
    public string? Description { get; set; } = description;

    public async Task Advise(MethodAsyncAdviceContext context)
    {

        //Execute my operations
        await context.ProceedAsync();
        
    }

}
@picrap
Copy link
Member

picrap commented Sep 13, 2024

Could you try

    public /* NOT async */ Task Advise(MethodAsyncAdviceContext context)
    {
        //Execute my operations
        return context.ProceedAsync();
    }

instead?

@Krummris
Copy link
Author

First of all, thanks for answering.

I changed the function, it still works normally when i run the app, but when my class with the attribute is called from the tests thats the error im getting

Identity.Infrastructure.Services.Tests.ServiceTests.CreateUser_DeveRetornarResponseComErro_QuandoFalhar:
Outcome: Failed
Error Message:
System.NullReferenceException : Object reference not set to an instance of an object.
Stack Trace:
at MyProject.DataAnnotations.WithActivitynewAttribute.Advise(MethodAsyncAdviceContext context) in /home/krummris/Documents/github/MyProject-sdk-develop/MyProject/DataAnnotations/WithActivityAttribute.cs:line 23
at ArxOne.MrAdvice.Advice.MethodAsyncAdviceContext.Invoke()
at ArxOne.MrAdvice.Invocation.ProceedAdvice2(Object target, Object[] parameters, RuntimeMethodHandle methodHandle, RuntimeMethodHandle innerMethodHandle, RuntimeMethodHandle delegatableMethodHandle, RuntimeTypeHandle typeHandle, Boolean abstractedTarget, Type[] genericArguments)
at ArxOne.MrAdvice.⚡Invocation.ProceedAspect(Object[], RuntimeMethodHandle, RuntimeMethodHandle, RuntimeMethodHandle)
at Identity.Infraestructure.Services.Service.CreateUser(User user)
at Identity.Infrastructure.Services.Tests.ServiceTests.CreateUser_DeveRetornarResponseComErro_QuandoFalhar() in /home/krummris/Documents/github/identity-sdk/Identity.UnitTests/Infraestructure/Services/ServiceTests.cs:line 63
--- End of stack trace from previous location ---

@picrap
Copy link
Member

picrap commented Sep 13, 2024

Sorry, I didn’t notice that it was specific to a test environment.
Did you try to debug to see what’s null?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants