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

[Question] MultiTenantContext now readonly #816

Open
malisancube opened this issue Apr 26, 2024 · 7 comments
Open

[Question] MultiTenantContext now readonly #816

malisancube opened this issue Apr 26, 2024 · 7 comments
Labels

Comments

@malisancube
Copy link

The change in v7.0.0 makes MultiTenantContext to be readonly preventing changing of currect tenant info.

image

More details fullstackhero/dotnet-starter-kit#925

Is there another way this can be done?

@AndrewTriesToCode
Copy link
Sponsor Contributor

AndrewTriesToCode commented Apr 26, 2024

Hi yes. It’s an implementation detail but you can inject IMultiTenantContextSetter. Look at the source in the middlewarere or TrySetTenantInfo and you’ll see how it can be used. I can’t recall right now but if it is only internal then I will make it public.

@malisancube
Copy link
Author

Impressive!

@douggish
Copy link

douggish commented May 2, 2024

I just ran into this same issue. I see IMultiTenantContextSetter is documented as an "implementation detail". Is there any plan to expose this functionality for general use? We are using the library in scenarios where the "entry point" is not an HTTP request.

@AndrewTriesToCode
Copy link
Sponsor Contributor

Yes it is available to inject as an interface currently. For your use case it is the right approach (or you could mutate the tenant info on the context but I don’t recommend that).

@fbjerggaard
Copy link

@AndrewTriesToCode Sorta related to this, when having a design time factory, how are we now supposed to inject the IMultiTenantContextAccessor? Earlier it was just a matter of creating a ITenantInfo and injecting, but since IMultiTenantContextAccessor only has internal implementations I can't really wrap my head around a solution - and the docs doesn't seem to have been updated to reflect this change

@goforebroke
Copy link

Hi Andrew,

Prior to the upgrade, I handled non http requests with something like this

 using var scope = _serviceProvider.CreateScope();

 var accessor = scope.ServiceProvider.GetRequiredService<IMultiTenantContextAccessor<AppTenantInfo>>();

 var multiTenantContext = new MultiTenantContext<AppTenantInfo>
 {
     TenantInfo = new AppTenantInfo()
     {
         Id = context.Message.TenantId.ToString(),
         Identifier = context.Message.Identifier.ToString(),
     }
 };

 acccessor.MultiTenantContext = multiTenantContext;

using the new interface, I will handle non http request as below?

 using var scope = _serviceProvider.CreateScope();

 var accessor = scope.ServiceProvider.GetRequiredService<IMultiTenantContextAccessor<AppTenantInfo>>();
 var setter = scope.ServiceProvider.GetRequiredService<IMultiTenantContextSetter>();

 var multiTenantContext = new MultiTenantContext<AppTenantInfo>
 {
     TenantInfo = new AppTenantInfo()
     {
         Id = context.Message.TenantId.ToString(),
         Identifier = context.Message.Identifier.ToString(),
     }
 };

 setter.MultiTenantContext = multiTenantContext;

@AndrewTriesToCode
Copy link
Sponsor Contributor

AndrewTriesToCode commented May 15, 2024

Hi @fbjerggaard and @goforebroke

Check out the Identity sample where I have the db design time factory in the data folder and and per-tenant scope for migration in program.cs.

I kept the old constructor signature for the mult-tenant db contexts specifically for these use cases despite the change in dependency injection. I should maybe update the docs on this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants