NullReferenceError using AutoQuery with ServiceStack 8.4

I have created a minimal ServiceStack 8.4 solution that highlights a NullReferenceError using AutoQuery in a service implementation.

Source code: Minimal project

If I use the required style for .NET 8, the IAutoQuery variable is null in the QueryAlbums service implementation. I need to use the latest coding practice to use the new ServiceStack Jobs feature.

This issue is a follow on from my previous issue:Unable to resolve service for type 'ServiceStack.Jobs.IBackgroundJobs

Code snippets

// Program.cs

// 1. Comment out to make it work  (pre .NET8 style)
builder.Services.AddServiceStack(typeof(AutoQueryIssue.ServiceInterface.MyServices).Assembly);
public class AppHost : AppHostBase, IHostingStartup
{
    // 2. Uncomment to make it work (pre .NET8 style)
    //public AppHost() : base("AutoQueryIssue", typeof(MyServices).Assembly) {}

    // 3. Comment out to make it fail (.NET8 style)
    public AppHost() : base("AutoQueryIssue") {}

    //...
}
public class ChinookServices : Service
{
	public IAutoQueryDb AutoQuery { get; set; } = null;

    public object Any(QueryAlbums request)
    {
		    using var db = AutoQuery!.GetDb<Albums>(Request);
		    var q = AutoQuery.CreateQuery(request, Request);
		    var response = AutoQuery.Execute(request, q, Request);
		    return response;
    }
}

Using the NET8 style, the AutoQuery in ChinookServices is null, causing a NullReferenceError.

Would greatly appreciate a solution.

As mentioned earlier you need to use constructor injection when using ASP.NET Core IOC

public class ChinookServices(IAutoQuery autoQuery) : Service
{
    public object Any(QueryAlbums request)
    {
        using var db = autoQuery.GetDb<Albums>(Request);
        var q = autoQuery.CreateQuery(request, Request);
        var response = autoQuery.Execute(request, q, Request);
        return response;
    }
}

Yes, that worked.

I did not consider your previous mention and I kept following the documentation at AutoQuery RDBMS, which does not use constructor injection.

Once again, thank you for the great support.

Was mentioned in the previous comment, basically you’ll need to update all your Services to always use constructor injection.

That’s what’s now required for all current Identity Auth and any new templates which are going to use Endpoint Routing and ASP .NET Core IOC.