servicestackfunq

ServiceStack IServiceGateway in Non Service/Non Controller and IOC


I have a console app with hangfire and service stack services into. Hangfire has its own IOC Adapter Implementations which has been integrated into a Funq adapter. I'm trying to use an IGatewayService to make calls to an inproc service.

The gateway is always null.

public class Tests
{
    public IServiceGateway gateway { get; set; }
    public Tests()
    {
        gateway =  HostContext.TryResolve<IServiceGateway>(); //tried this along with every type of registration

    }
    public void Test3() {
       // I want to use gateway here to call an inproc service
    }
}

I've tried:

Container.Register<IServiceGatewayFactory>(x => new TestServiceGatewayFactory())
            .ReusedWithin(ReuseScope.None);

            Container.Register<Tests>(new Tests() { gateway = Container.Resolve<IServiceGateway>() });

And some other non funq adapters and the gateway is always null. I could create the gateway in the register new TestServiceGateway() but it requires an IRequest. If I pass null there it won't work either.

The hangfire call is simple:

RecurringJob.AddOrUpdate<Tests>((t) => t.Test3(), Cron.Yearly(12, 12));

Solution

  • The API to get the Service Gateway is:

    HostContext.AppHost.GetServiceGateway(Request);
    

    Where Request is the ServiceStack IRequest which you can create from an ASP.NET Request outside of ServiceStack with:

    var request = HttpContext.Current.ToRequest();
    

    Or you can create a Mock Request with:

    var request = new MockHttpRequest();