"Value cannot be null. (Parameter 'key')

I can’t resolve this error. It occurs when running a self hosted servicestack service.
If I initialize a request using

var service = appHost.Resolve<MovieService();
service.Request = new MockHttpRequest
        {
            XRealIp = "192.168.1.10",
            IsLocal = true,
            Headers = RequestHeaders,
            Authorization = ApiKey
        };

var response = service.Gateway.Send(new SomeRequest{Id = 1});

and then within that method call other services using subsequent Gateway.Send I get an error like the following.

[GetDbMovie: 4/30/2020 11:37:11 PM]:
[REQUEST: {id:220725}]
System.ArgumentNullException: Value cannot be null. (Parameter ‘key’)
at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value)
at ServiceStack.ApplyToUtils.HttpMethodAsApplyTo(IRequest req) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\ApplyTo.cs:line 57
at ServiceStack.RequestFilterAsyncAttribute.RequestFilterAsync(IRequest req, IResponse res, Object requestDto) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\RequestFilterAttribute.cs:line 65
at ServiceStack.Host.ServiceRunner`1.ExecuteAsync(IRequest req, Object instance, TRequest requestDto) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\ServiceRunner.cs:line 125

The initial Service has [Authenticate] attribute and subsequent methods have a combination of [Authenticate] and anonymous permissions.

It seems that I need to add something else to the request object so that whatever Dictionary it is looking at has the Key.

The Request DTO is as follows:

 [Authenticate][Restrict(VisibleInternalOnly = true)]
    [Route("/Movies/FromDb/{Id}", Verbs = "GET", Notes = "Requires a valid MovieId.", Summary = "Get Movie by Movie ID.")]
    public class GetDbMovie : IReturn<DbMovieResponse>
    {
        [ApiMember(IsRequired = true, Description = "Required ID of Movie.")]
        public long Id { get; set; }

        [ApiMember(IsRequired = false, Description = "List of References to be included in result.  Ex: 'Genres,Contributors,Videos' ")]
        public string[] References { get; set; }
    }

The initial DTO looks like:

 [Authenticate][Restrict(VisibleInternalOnly = true)]
    [Route("/AlphaMerge/CompareMovie", Verbs = "GET", Summary = "")]
    public class CompareMovie : IReturn<CompareResponse>
    {
        [ApiMember(Description = "Entertainment Movie ID.", IsRequired = true)]
        public long Id { get; set; }

        [ApiMember(Description = "")]
        public DateTime? AllowableIndexAgeDate { get; set; }
    }

regards,
Bob

Note: Any implementation attributes like the [Authenticate] attribute should be on the Service class (instead of the DTO) to avoid coupling to implementation assemblies.

Following the StackTrace, the Exception is thrown here:

i.e. Verb is empty, but why are you using a MockHttpRequest in a Service Gateway?

Thanks for the response.

I am setting the service’s Request object with a MockRequest in order to use the Gateway. This is a console application to handle some batch processing not a web server so there is no initial web request. If I attempt to use the Gateway without setting a request on the service, I get a req required error.

I found my issue. There are several helper methods that are called along the process and one wasn’t using the service’s gateway.

Thanks for your help!!

1 Like