Stephen Brannan - 140 - Feb 19, 2014

What is the best way to create a new user on initialization of servicestack? I tried r = appHost.TryResolve<RegisterService>(); and then r.Post(new Register() {/* set props here */}); but that gave me an object reference not set because no session/irequest exists yet. 

Use the registered IUserAuthRepository and create users with CreateUserAuth.

Stephen Brannan:

Thanks Demis. I’ll use that instead. Out of curiosity is there a way post initialization to access a service like I was trying to do?

Yeah different ways of resolving a Service: http://stackoverflow.com/a/21446458/85785

Note: if your Service requires access to the HttpRequest and you’re not calling from within a HttpRequest, the Request will be null and will likely throw a NRE when trying to access it.

Stephen Brannan:

Yup I know how to get to the service… but is there a way to have a post initialization event/delegate where a HttpRequest is available? If not, does this sound like a reasonable feature request?

Not sure what you mean, how can a HttpRequest be available if it’s not originating from an actual HTTP Request?

Stephen Brannan:

Hmm I see what you mean and without a real use case it’s difficult to come up with an example. Perhaps a way to setup a post init request (kind of like mocking the request solely for a post init event)… but even that sounds hackish to me. Just a thought. Either way thanks for the help Demis.

You can already use ServiceController.Execute*() methods to execute a request DTO and pass in your own IRequest context. 

ServiceController.Execute(requestDto) by default uses BasicRequest: 
https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/Host/ServiceController.cs#L478

Stephen Brannan:

Oh that is sweet. Exactly what I was thinking of. Thanks again Demis. You’ve thought of everything in this framework. :wink:

We’ll maybe not everything, but at least the things I’ve needed or thought were useful to have :slight_smile: