Accessing ASP.NET singletons

The documentation states we can access dependencies registered in ASP.NET in ServiceStack, but I wasn’t sure how they are accessed.

If we registered a dependency in ConfigureServices:

o = new UsefulClass()
services.AddSingleton(o);

Do we have to also register it in ServiceStack’s IoC? Or can it be automatically injected to our services without that?

No, they can be injected into ServiceStack Services and don’t need to be re-registered in Func IOC, i.e. anything you register in ASP .NET Core’s IServiceCollection can be resolved/injected in ServiceStack Services.

Dependencies registered in ServiceStack’s Func IOC can only be resolved by ServiceStack, e,g. they’re not accessible from MVC Controller constructors, although they can still be resolved via the HostContext.TryResolve<T>() static function.

thank you, one further question. Many of my services are very simple in that most of the logic is contained in static factory methods, ie:

[Authenticate]
class AdvReportHeadersService : Service
{
    public object Any(AdvReportHeaders req) =>
        AdvReportHeadersFactory.GetObject(req);
}

These factory methods are more likely where the dependencies would be useful- database connections, logging, etc. But it doesn’t look like injection works as well (or at all) on these factory classes. Is the only solution in that case to pass the needed depencies through as a parameter on GetObject()? Or is it a better practice to not use static factory methods in this way and keep most of the heavy lifting work in the IService class?

Not sure what you mean by “doesn’t look like injection works as well” as any dependencies registered should still be injected when resolving dependencies.

The IRequest context wont get injected and you would need to pass that in as you’re doing, but your other dependencies that you register in your IOC should still be injected.

What i mean is if I declare UsefulClass on the Service class above, the DI is working, but if I declare it on the static factory method it’s calling, it’s null in that static method.

[Authenticate]
class AdvReportHeadersService : Service
{
    public UsefulClass cls { get; set; }  //expecting DI to populate

    public object Any(AdvReportHeaders req) 
    {
        Console.WriteLine(cls.SomeStringProperty);    //DI works, cls is not null
        AdvReportHeadersFactory.GetObject(req);
     }
}

static class AdvReportHeadersFactory
{
    public static UsefulClass cls { get; set; }   //expecting DI to populate

    public static string GetObject(AdvReportHeaders req)
    {
        return cls?.SomeStringProperty ?? "di didn't work";   //DI doesn't work, cls is null
    }
}

ServiceStack is what is performing that injection on your Service classes since ServiceStack is instantiating the services to process requests for you. If you perform:

var myService = new AdvReportHeadersService();

The cls property will also be null as expected, as there is nothing (framework,system process,other code,etc) populating cls for you, just like there is nothing performing that injection for you in your static class example.

Hope that helps clear things up.

Well-explained, thank you.

I think I’ll gain the most benefit from injection by refactoring the static classes away and putting that logic in the service classes.