2017-04-19 20 views
1

我有一个控制台应用程序与hangfire和服务堆栈服务。 Hangfire有自己的IOC适配器实现,它已经集成到Funq适配器中。我正在尝试使用IGatewayService来调用inproc服务。ServiceStack IServiceGateway非服务/非控制器和IOC

网关始终为空。

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 
    } 
} 

我已经试过:

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

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

和其他一些非funq适配器和网关总是空。我可以在寄存器new TestServiceGateway()中创建网关,但它需要一个IRequest。如果我在那里传递null也不起作用。

的迟发型通话很简单:

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

回答

0

获得服务网关的API是:

HostContext.AppHost.GetServiceGateway(Request); 

哪里Request是ServiceStack IRequest你可以从ASP.NET请求之外创建ServiceStack与:

var request = HttpContext.Current.ToRequest(); 

O您可以创建一个模拟请求:

var request = new MockHttpRequest(); 
+0

如果没有HttpContext?这是运行在一个hangfire工作.. – lucuma

+0

@lucuma我已经更新了我的答案,包括一个'MockHttpRequest' – mythz

+0

谢谢,帮助我追踪它。我以'LightContainer.Register ((x)=> new Tests() {gateway = LightContainer.GetInstance ()。GetServiceGateway(new MockHttpRequest())});'这同样适用于funq只是我使用LightInject和Funq。 – lucuma