1

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));
lucuma
  • 18,247
  • 4
  • 66
  • 91

1 Answers1

0

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();
mythz
  • 141,670
  • 29
  • 246
  • 390
  • If there is no HttpContext ? This is running inside of a hangfire job.. – lucuma Apr 19 '17 at 21:55
  • @lucuma I've updated my answer to include a `MockHttpRequest` – mythz Apr 19 '17 at 22:00
  • Thank you that helped me track it down. I ended up with `LightContainer.Register((x)=>new Tests() { gateway= LightContainer.GetInstance().GetServiceGateway(new MockHttpRequest())});` The same would have worked with funq just that I am using both LightInject and Funq. – lucuma Apr 19 '17 at 22:12