Reputation: 75
I have an IIS hosted app which takes an id from query string and instantiates user object like so. In AppHost.Configure I register UserService in IoC like so
container.Register(x => new UserService(x.Resolve<IDataContext>()).GetUser()).ReusedWithin(ReuseScope.Request);
My UserService (this is not a ServiceStack service, just an internal helper) looks like
public class UserService
{
public UserService(IDataContext dataContext)
{
_dataContext = dataContext;
}
public User GetUser()
{
var uid = HttpContext.Current.Request.QueryString["$id$"];
//snip
}
//snip
}
I want to change this to a self hosted app and therefore I don't have access to HttpContext any more. I have looked at HttpListenerContext but nothing seems to be populated when my class is being injected.
Can anyone shed any light on how to pass param from query string to this class?
Upvotes: 2
Views: 143
Reputation: 143284
You can't access any dependencies in your constructor and every ServiceStack action requires a Request DTO see the New API wiki for more details.
In your Service actions you can simply access the base.Request
and base.Response
when you need them e.g:
public class MyService : Service
{
public UserService UserService { get; set; }
public User Get(MyRequest request)
{
var user = UserService.GetUser(base.Request);
//snip
}
//snip
}
Note: there is no singleton for HttpListenerContext for Self-Hosting, so you can't get access to the runtime HTTP Request from inside an IOC lambda.
Upvotes: 1