Suraj
Suraj

Reputation: 271

Can I access webservice from within a custom user session in service stack?

I have the standard Hello World web service, with my custom user auth session, because I wanted some additional parameters. The authentication part works 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 the refId holding a custom parameter from one of my other tables, and it gets the right value when I debug. My question is, can I now call methods of the webservice from within this method? So, for example, if I had an execute method that accepted an int, can I call it from within the overridden OnAuthenticated method?

Upvotes: 3

Views: 222

Answers (2)

mythz
mythz

Reputation: 143319

You can execute a Service in ServiceStack using ResolveService<T>.

From inside a custom user session:

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

From inside a ServiceStack Service:

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

From outside of ServiceStack:

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

Upvotes: 2

user270576
user270576

Reputation: 997

Try resolving ServiceInterface.IServiceBase to proper 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));

Upvotes: 0

Related Questions