System.Reflection.TargetInvocationException

Hello,

On metadata related endpoints (/types/*, the new /metadata/app), I’m getting a System.Reflection.TargetInvocationException (Inner Exceptions: The type initializer for ‘ServiceStack.TypeProperties`1’ threw an exception, and NullReferenceException: Object reference not set to an instance of an object.) This is new as of 5.9…

I’m assuming something is up with my models, but what should I be looking for exactly? Any breakpoints I can set in the SS source to help figure this out?

Please post the full Exception StackTrace.

The best way to define the issue is to comment out your Service classes until you can identify which Service class & then which Service method is causing the issue.

If I break before NullTracer.WriteError:

   at ServiceStack.Text.EmitReflectionOptimizer.CreateGetter(PropertyInfo propertyInfo) in C:\BuildAgent\work\912418dcce86a188\src\ServiceStack.Text\ReflectionOptimizer.Emit.cs:line 45
   at ServiceStack.TypeProperties`1..cctor() in C:\BuildAgent\work\912418dcce86a188\src\ServiceStack.Text\TypeProperties.cs:line 40

After that it winds up in Framework source code:

   at System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized)
   at System.Reflection.RtFieldInfo.UnsafeGetValue(Object obj) in f:\dd\ndp\clr\src\BCL\system\reflection\fieldinfo.cs:line 690

I think I figured it out though by disabling JIT optimization while debugging and walking through the call stack… I had a service model set to IReturnVoid, but the service call response was set to Task<HttpResult>. I use HttpResult to customize the response status code, should I be doing something different?

HttpResult is supposed to be for returning a customized HTTP Response, but it should still allow be supported with a null response. See 4) for how to return a custom short-circuited HTTP response.

Can you please provide an example Request DTO + Service method impl that repro’s this issue?

Request DTO:

[Route("/sccm/collections/{CollectionId}/members", "POST")]
    public class CreateSccmCollectionMembership : IReturnVoid
    {
        public string CollectionId { get; set; }
        public string[] ComputerNames { get; set; }
    }

Service Method:

 public async Task<HttpResult> Any(CreateSccmCollectionMembership request)
        {
            List<SccmSystem> systems = request.ComputerNames.Select(computer => Db.Single<SccmSystem>(x => x.Name == computer)).ToList();
            if (systems.Count <= 0)
            {
                throw new HttpError(HttpStatusCode.BadRequest, "Machines not found in SCCM.");
            }

            await SccmManagementUtilities.AddMembersToSccmCollection(request.CollectionId, systems);
            return new HttpResult(HttpStatusCode.Created, "Machines Added to collection");
        }

I can’t repro this, I don’t have access to your impl classes so I’ve converted it into an async function like:

[Route("/sccm/collections/{CollectionId}/members", "POST")]
public class CreateSccmCollectionMembership : IReturnVoid
{
    public string CollectionId { get; set; }
    public string[] ComputerNames { get; set; }
}

public class MyServices : Service
{
    public async Task<HttpResult> Any(CreateSccmCollectionMembership request)
    {
        await Task.Delay(1);
        return new HttpResult(HttpStatusCode.Created, "Machines Added to collection");
    }
} 

Only the declaration & signatures impact schema so this should be enough to repro the issue, but all the metadata & code-generation APIs are working without issue.

Can you provide a stand-alone Service class that repro’s the issue in an empty web project, e.g:

$ x new web Repro

Yep, I’ll work on this next week and get back to you.

1 Like

Setting up the repro led me to discover where the problem is. I believe you’ll be able to reproduce it if you turn on Env.StrictMode in Global.asax… If you need the full repro project let me know, but that seems to be the trigger.

Great thx, was able to catch & identify the Exception. This should now be resolved from the latest v5.9.1 that’s now available on MyGet.

1 Like