【问题标题】:ServiceStack IServiceGateway in Non Service/Non Controller and IOC非服务/非控制器和 IOC 中的 ServiceStack IServiceGateway
【发布时间】:2017-04-19 21:35:27
【问题描述】:

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

网关始终为空。

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 它也不会工作。

hangfire 调用很简单:

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

【问题讨论】:

    标签: servicestack funq


    【解决方案1】:

    获取Service Gateway的API是:

    HostContext.AppHost.GetServiceGateway(Request);
    

    其中Request 是ServiceStack IRequest,您可以使用ServiceStack 之外的ASP.NET 请求创建它:

    var request = HttpContext.Current.ToRequest();
    

    或者你可以创建一个模拟请求:

    var request = new MockHttpRequest();
    

    【讨论】:

    • 如果没有 HttpContext ?这是在hangfire作业中运行的..
    • @lucuma 我已经更新了我的答案,加入了MockHttpRequest
    • 感谢您帮助我找到它。我最终得到了LightContainer.Register&lt;Tests&gt;((x)=&gt;new Tests() { gateway= LightContainer.GetInstance&lt;IServiceGatewayFactory&gt;().GetServiceGateway(new MockHttpRequest())}); 这同样适用于 funq,只是我同时使用 LightInject 和 Funq。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 2013-07-06
    • 2012-02-25
    相关资源
    最近更新 更多