Multi-tenant ServiceStack API, same deployment to respond to requests on different hostnames?

前端 未结 1 1741
青春惊慌失措
青春惊慌失措 2020-12-30 09:58

We\'re creating APIs using ServiceStack that are multi-tenant. We want to do DNS-based load-balancing and routing, rather than stitch things up via a reverse proxy (like ng

相关标签:
1条回答
  • 2020-12-30 10:50

    I solved this just this week, on a existing multi-tenant system which uses .NET security principals to deal with the user permissions and tenants. I used a custom ServiceRunner to select the tenant and set up the security. Your approach to multi-tenant is different, but using a ServiceRunner still seems a valid approach.

    You'd end up with something like this:

    public class MyServiceRunner<T> : ServiceRunner<T>
    {
        public MyServiceRunner(IAppHost appHost, ActionContext actionContext)
            : base(appHost, actionContext)
        {}
    
        public override void BeforeEachRequest(IRequestContext requestContext, T request)
        {
            // Set backend authentication before the requests are processed.
            if(request instanceof ITenantRequest)
            {
                Uri uri = new Uri(requestContext.AbsoluteUri);
                string tenant = uri.Host; // Or whatever logic you need...
                ((ITenantRequest).Tenant = tenant;
            }
        }
    }
    
    public class MyAppHost : AppHostBase
    {
        public MyAppHost() : base("My Web Services", typeof(MyService).Assembly) { }
    
        public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
        {
            return new MyServiceRunner<TRequest>(this, actionContext);
        }
    
        public override void Configure(Container container)
        {
            ...
        }
    }
    

    Perhaps the Requests filtering approach is somehow better, but this does the job for us.

    0 讨论(0)
提交回复
热议问题