RegisterService not working when called via service in MVC

Hi

I have some problems using a derived RegisterService (ServiceStack.Auth) when called from inside my ServiceStack host.

My setup:

  • standard net core installation of ServiceStack and MVC
  • I have a service defined which I resolve (TryResolve in the controller)
  • service is resolved, and within that service I resolve the internal RegisterService

With that I get following error:

This AppHost does not support accessing the current Request via a Singleton

Any idea how to resolve this.
For the moment I am “fixing” it by creating a JsonHttpClient and then call the same service via the rest interface but this seems a bit of overkill.

Are you passing in the IRequest when calling ServiceStack Services in MVC?

public HelloController : ServiceStackController 
{
    public void Index(string name) 
    {
        using (var hello = base.ResolveService<HelloService>())
        {
           ViewBag.GreetResult = hello.Get(name).Result;
           return View();
        }
    }        
}

Can you please provide the source code you’re using + full StackTrace.

Hi

Thanks for the tip. That was indeed the issue!
To be complete, on .net core I needed to pass this.ServiceStackRequest instead of base.HttpContext

1 Like

Yep, if you’re in a controller you can also call the base method without args, e.g:

using (var hello = base.ResolveService<HelloService>())
{
   ViewBag.GreetResult = hello.Get(name).Result;
   return View();
}

Outside an MVC Controller or ServiceStack Service you would need to pass a ServiceStack Request:

using (var hello = HostContext.ResolveService<HelloService>(ServiceStackRequest))
{
   ViewBag.GreetResult = hello.Get(name).Result;
   return View();
}

Thanks.
Still having issue, but not related to the mentioned error.

Because I am calling the RegisterService (basically want to create a new user for application), I now am logged out from the website… Most probably the RegisterService is performing a login? I passed the AutoLogin = false to the service but it seems to clear my cookie in the admin portal. Any suggestions?

http://mvc.netcore.io shows an example of Registering / Authenticating with MVC. Although it calls ServiceStack’s /register Service directly.

The Alternative is to instead of calling RegisterService to create the User directly from the AuthRepository with:

var newUser = base.AuthRepository.CreateUserAuth(newUserAuth, request.Password);
1 Like