Access registered object in Nancy Startup class

177 Views Asked by At

I'm writing a service in Nancy and I'm using some middleware for monitoring the service:

    app.UseOwin(buildFunc => 
    { 
        var log = ConfigureLogger();
        buildFunc.UseMonitoringAndLogging(log, HealthCheck);
        buildFunc.UseNancy();
    });

The middleware is configured to use a HealthCheck() function defined in the Startup class as:

    public async Task<bool> HealthCheck()
    {
        return await SomeRepo.HealthCheck();
    }

SomeRepo has a HealthCheck() method that queries the database to confirm it is available/responding. But how to inject SomeRepo into the Startup class, or alternatively access the container to resolve SomeRepo?

1

There are 1 best solutions below

2
Sifiso Shezi On

At this point you are still in Owin and not in the Nancy pipeline. What host are you running on ? If you are using aspnetcore the you can register your deps in RegisterServices() method and it will handle the injection into your method. You can use Autofac or StructureMap so you can share your container with Nancy as well like this.