Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Tenant Identification Service to dynamically resolve database connection strings based on 'X-Client-ID' header #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

rajibmahata
Copy link

Implemented Tenant Identification Service to dynamically resolve database connection strings based on 'X-Client-ID' header. Removed middleware dependency for connection resolution to ensure accurate handling during request lifecycle. Added support for dynamic DbContext creation for multi-tenant architecture

…equest. If a valid client ID (e.g., ClientA or ClientB) is found, it retrieves the corresponding connection string from the application configuration. If the client ID is missing or invalid, the request is rejected.
@rajibmahata
Copy link
Author

@microsoft-github-policy-service agree

@rajibmahata
Copy link
Author

How It Works

Tenant Identification Service:
We created a service that checks the X-Client-ID header in the HTTP request. If a valid client ID (e.g., ClientA or ClientB) is found, it retrieves the corresponding connection string from the application configuration. If the client ID is missing or invalid, the request is rejected.

DbContext Factory:
Instead of configuring the database context (DbContext) during application startup, we now dynamically create it at runtime. A custom factory ensures that the correct connection string, based on the identified client, is used to configure the DbContext.

Request Lifecycle:
Each request now goes through the Tenant Identification Service, which dynamically resolves the connection string. The service is scoped to the request lifecycle, ensuring each client’s request is handled with its specific configuration.

Example Use Case:
When a request is made with X-Client-ID: ClientA, the service retrieves ClientA’s connection string and passes it to the DbContext. This can be changed dynamically by altering the header value in the request. Similarly, requests with X-Client-ID: ClientB will use ClientB’s database.

@rajibmahata rajibmahata closed this Jan 9, 2025
@rajibmahata rajibmahata deleted the Task-HandleDynamicConnectionStrings#15 branch January 9, 2025 06:24
@rajibmahata rajibmahata restored the Task-HandleDynamicConnectionStrings#15 branch January 9, 2025 06:44
@rajibmahata rajibmahata reopened this Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant