abp icon indicating copy to clipboard operation
abp copied to clipboard

How to send tenantid to downstream services when using ocelot with abp

Open slarkerino opened this issue 5 years ago • 3 comments

I am currently using ABP and ocelot to build a api gateway for microservices. Everything works fine but now I want to use api gateway as a domain resolver. ABP's Domain Resolver works to resolve tenantid from different domains but i want ocelot to able to send the resolved tenant id to the downstream services(Include __tenant header in the headers of the request to the downstream service).

slarkerino avatar Jun 22 '21 05:06 slarkerino

Maybe you need to ask the ocelot team how to do it.

realLiangshiwei avatar Jun 22 '21 06:06 realLiangshiwei

https://stackoverflow.com/questions/68078156/how-to-send-tenantid-to-downstream-services-when-using-ocelot-with-abp/68114482#68114482

You can create a Middleware for adding ResolvedId to the request headers. So the ocelot will send the edited request to downstream with the tenantId.

Example:

public class MyMiddleware : IMiddleware, ITransientDependency
    {
        private readonly ICurrentTenant CurrentTenant;

        public MyMiddleware(ICurrentTenant currentTenant)
        {
            CurrentTenant = currentTenant;
        }

        public Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            context.Request.Headers.Add("__tenant", CurrentTenant.Id.ToString());

            return next(context);
        }
    }

As you see, the middleware manupilates the request, it's adding the CurrentTenantId to the request. The CurrentTenant is found before by your "domain tenant resolver".

You can use this middleware between app.UseMultiTenancy() & app.UseOcelot()

...
app.UseMultiTenancy();

app.UseMiddleware<MyMiddleware>(); // it must be here, between them

app.UseOcelot();
...

cotur avatar Jun 24 '21 11:06 cotur

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 23 '21 14:08 stale[bot]