如何在自托管应用程序中访问查询字符串参数

时间:2013-04-10 12:53:35

标签: servicestack

我有一个IIS托管的应用程序,它从查询字符串中获取id并实例化用户对象。在AppHost.Configure中,我在IoC中注册了UserService,如此

container.Register(x => new UserService(x.Resolve<IDataContext>()).GetUser()).ReusedWithin(ReuseScope.Request);

我的UserService(这不是ServiceStack服务,只是一个内部帮助器)看起来像

    public class UserService
    {
        public UserService(IDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public User GetUser()
        {
            var uid = HttpContext.Current.Request.QueryString["$id$"];
            //snip
        }
        //snip
    }

我想将此更改为自托管应用,因此我无法再访问HttpContext。我查看了HttpListenerContext,但是当我的类被注入时似乎没有填充。

任何人都可以了解如何将查询字符串中的param传递给此类吗?

1 个答案:

答案 0 :(得分:1)

您无法访问构造函数中的任何依赖项,并且每个ServiceStack操作都需要请求DTO才能查看New API wiki以获取更多详细信息。

在您的服务操作中,您只需在需要时访问base.Requestbase.Response,例如:

public class MyService : Service
{
    public UserService UserService { get; set; }

    public User Get(MyRequest request)
    {
        var user = UserService.GetUser(base.Request);
        //snip
    }
    //snip
}

注意:HttpListenerContext没有用于自托管的单例,因此您无法从IOC lambda内部访问运行时HTTP请求。