Object reference not set to an instance of an object When calling a function with Attribute From Test Project
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.<ProceedAdvice2>b__0(Task t)
at ArxOne.MrAdvice.Threading.TaskContinuer1.<>c__DisplayClass0_01.<ContinueWith>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();
}
}
Could you try
public /* NOT async */ Task Advise(MethodAsyncAdviceContext context)
{
//Execute my operations
return context.ProceedAsync();
}
instead?
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 ---
Sorry, I didn’t notice that it was specific to a test environment.
Did you try to debug to see what’s null?
Are you including MrAdvice as a Package Reference in your test project too? And any static setup that may be performed on the Aspect is also being done in the test?
These are the 2 issues I encountered when testing and fixing these fixed all my testing issues