简体   繁体   中英

ServiceStack.RequestFilterAsyncAttribute error: System.ArgumentNullException: Value cannot be null. Parameter name: method

I got runtime error and also find a way to fix (in the end). Just curious why cannot use public property for constructor. RequestFilterAsyncAttribute or RequestFilterAttribute doesn't matters, btw.

Exception case

public class CustomTokenFilterAttribute : RequestFilterAsyncAttribute
{
    public CustomTokenFilterAttribute() {}
    public CustomTokenFilterAttribute(params string[] roles)
    {
        RequireRoles = roles?.ToList();
    }

    public override async Task ExecuteAsync(IRequest req, IResponse res, object requestDto) {}

    public List<string> RequireRoles { get; private set; } // public property: ERROR here
}
System.ArgumentNullException: Value cannot be null.  Parameter name: method
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression arg0)
at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable`1 arguments)
at Funq.Container.GenerateAutoWireFnForProperty(Container container, MethodInfo propertyResolveFn, PropertyInfo property, Type instanceType) in C:\\BuildAgent\\work\\3481147c480f4a2f\\src\\ServiceStack\\Funq\\Container.Adapter.cs:line

However, this way will solve the problem.

public class CustomTokenFilterAttribute : RequestFilterAsyncAttribute
{
    public CustomTokenFilterAttribute() {}
    public CustomTokenFilterAttribute(params string[] roles)
    {
        RequireRoles = roles?.ToList();
    }

    public override async Task ExecuteAsync(IRequest req, IResponse res, object requestDto) {}

    readonly List<string> RequireRoles; // private property: ok
}

Because the IOC is trying to inject the dependency into public properties with setters.

You can change it to a readonly public property without a setter, eg:

public class CustomTokenFilterAttribute : RequestFilterAsyncAttribute
{
    public List<string> RequireRoles { get; } 
    public CustomTokenFilterAttribute(params string[] roles)
    {
        RequireRoles = roles?.ToList();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM