ServiceStack:访问Service中的IRequest返回null [英] ServiceStack: Accessing the IRequest in the Service returns null

查看:74
本文介绍了ServiceStack:访问Service中的IRequest返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Servicestack.我的服务有一个基类,如下所示:

I am using Servicestack. I have a base class for my Services, like so:

public abstract class ServiceHandlerBase : Service

然后是一些感兴趣的方法和属性.我已经有几种访问 IRequest 对象的方法,例如:

and then some methods and properties in there of interest. I already have several methods that accesses the IRequest object, like:

    protected AlfaOnline GetContactItem()
    {
        string deviceUUID = Request.Headers.Get(Constants.DEVICE_UUID); // <-- calling this method from constructor will give NullRef on Request here
        string authToken = Request.Headers.Get(Constants.AUTH_TOKEN);
        // do stuff
        return existingContactItem;
    }

在我的服务实现中运行良好,没有问题.

which works well inside my service implementations, no problems there.

现在,我想使用这个完全相同的方法直接从基类,在构造函数中调用它:

Now, I wanted to use this exact same method directly from the base class, calling it in the constructor:

    public ServiceHandlerBase()
    {
        AlfaOnline ao = GetContactItem();
    }

但是我在 Request 对象上得到了一个 NullReferenceException 如上所述.

but I then get a NullReferenceException on the Request object as noted above.

Request 对象什么时候可以访问和使用?因为它在服务实现中不为空.

When is the Request object ready to access and use? Because it's not null inside the service implementations.

推荐答案

在注入之前,您不能在构造函数中访问任何依赖项,例如 IRequest,它们只能在注入之后访问Service 类已经被初始化,就像你的 Service 方法被调用一样.

You can't access any dependencies like IRequest in the constructor before they've been injected, they're only accessible after the Service class has been initialized like when your Service method is called.

您可以使用 Custom Service Runner 在任何服务被执行,例如:

You can use a Custom Service Runner to execute custom logic before any Service is Executed, e.g:

public class MyServiceRunner<T> : ServiceRunner<T> 
{
    public override void OnBeforeExecute(IRequest req, TRequest requestDto) {
      // Called just before any Action is executed
    }
}

并在您的 AppHost 中使用 ServiceStack 注册它:

And register it with ServiceStack in your AppHost with:

public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext ctx)
{           
    return new MyServiceRunner<TRequest>(this, ctx);
}

但如果您只想为 Service 类运行一些逻辑,您现在可以覆盖基类中的 OnBeforeExecute(),例如:

But if you just want to run some logic for a Service class you can now override OnBeforeExecute() in your base class, e.g:

public abstract class ServiceHandlerBase : Service
{
    public override void OnBeforeExecute(object requestDto)
    {
        AlfaOnline ao = GetContactItem();
    }
}    

参见 ServiceFilterTests.cs 一个工作示例.

See ServiceFilterTests.cs for a working example.

如果您正在实施 IService 而不是继承 Service 基类,您可以实施 IServiceBeforeFilter.

If you're implementing IService instead of inheriting the Service base class you can implement IServiceBeforeFilter instead.

新的服务过滤器从 v5.4.1 开始可用,现在 MyGet 上提供.

The new Service Filters is available from v5.4.1 that's now available on MyGet.

这篇关于ServiceStack:访问Service中的IRequest返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆