Can I access the web service from a user session on the service stack?

I have a standard Hello World web service with my custom auth session because I need additional parameters. The authentication part is working as expected. Below is my CustomUserSession:

public class CustomUserSession : AuthUserSession
{
    public int? PortalId { get; set; }

    public override void OnAuthenticated(ServiceInterface.IServiceBase authService, IAuthSession session, IOAuthTokens tokens, Dictionary<string, string> authInfo)
    {
        base.OnAuthenticated(authService, session, tokens, authInfo);
        var refId = session.UserAuthId;

        var userRep = new InMemoryAuthRepository();
        var userAuth = userRep.GetUserAuth(refId.ToString());
        PortalId = userAuth.RefId;
    }
}

      

I have a refId containing a custom parameter from one of my other tables and it gets the correct value when debugging. My question is, can I now call web service methods from this method? For example, if I had an execute method that took an int, can I call it from the overridden OnAuthenticated method?

+3


source to share


2 answers


You can execute a service on ServiceStack with ResolveService<T>

.

Inside the user's user session:

using (var service = authService.ResolveService<MyService>())
{
    var response = service.Get(new MyRequest { ... });
}

      

Inside ServiceStack:



using (var service = base.ResolveService<MyService>())
{
    var response = service.Get(new MyRequest { ... });
}

      

Outside ServiceStack:

using (var service = HostContext.ResolveService<MyService>())
{
    var response = service.Get(new MyRequest { ... });
}

      

+2


source


Try to resolve the ServiceInterface.IServiceBase

correct class / interface. Here's an example for IDbConnectionFactory

:



//Resolve the DbFactory from the IOC and persist the info
authService.TryResolve<IDbConnectionFactory>().Exec(dbCmd => dbCmd.Save(PortalId));

      

0


source







All Articles