Query returns no results and NullReferenceException
Environment Details
- Finbuckle.MultiTenant Version: 9.0.0
- EF Core Version: 9.0.0
- Database Provider: SQL Server
- .NET Version: .NET 9
Description
I'm implementing a shared database multi-tenancy sample project using the Finbuckle.MultiTenant library (GitHub). While setting it up, I encountered the following two issues:
Issue 1: Query Returns No Results
Despite having companies populated for a valid tenant (e.g. tenant1), no data is returned from the queries. Here are some screenshots for context:
Issue 2: NullReferenceException for Invalid Tenant
When an invalid tenant identifier (e.g. asd) is passed, the application throws a NullReferenceException.
Screenshot:
Any thoughts on these issues? I could've debugged the source code myself, but I feel it’s better to ask this question at this point, as the documentation is somewhat outdated or limited in certain areas. I’d appreciate feedback on whether my implementation aligns with the intended usage.
Source Code
hello @Hulkstance, by any chance are you using minimal API's?
This was happening to me because the tenant was not being resolved. In my case its because of the usage of minimal API's but there are many reasons this might be failing.
If you are using minimal API's i created a middleware to handle this for me which I've provided below.
public class TenantResolutionMiddleware(RequestDelegate next)
{
public async Task InvokeAsync(HttpContext httpContext, AppDbContext tenantStore)
{
var routeTenantId = httpContext.Request.RouteValues["tenantId"]?.ToString();
if (!string.IsNullOrEmpty(routeTenantId))
{
var tenantInfo = await tenantStore.AppTenantInfo.FindAsync(routeTenantId);
if (tenantInfo != null)
{
httpContext.SetTenantInfo(tenantInfo, true);
}
}
await next(httpContext);
}
}
you will have to modify this to support the kind of store your using but this was much easier then moving from minimal API's back to controllers.
@Hulkstance thanks for the detail you provided. I’m on vacation right now and will take a closer look next week.
@funkel1989 thanks for chiming in
Thank you for the replies!
@AndrewTriesToCode, have a great one!
@funkel1989, the NullReferenceException thrown by the EF Core global filter was a bit generic, so I spent some time debugging the call stack.
The issue was with this call in TenantResolver.cs:
var tenantInfo = await wrappedStore.TryGetByIdentifierAsync(identifier);
It returned null because I had used await tenantStore.TryGetAsync in the ConfigureJwtBearerOptions.cs where I set context.HttpContext.SetTenantInfo(tenant, true);. That was an unfortunate oversight.
It works fine now. However, I still wonder if I have implemented this part https://github.com/Hulkstance/multi-tenancy/blob/main/src/SharedDatabase.Infrastructure/Auth/Jwt/ConfigureJwtBearerOptions.cs#L59 and https://github.com/Hulkstance/multi-tenancy/blob/main/src/SharedDatabase.Infrastructure/MultiTenancy/CurrentTenantService.cs correctly. If @AndrewTriesToCode you could take a look whenever you get a chance
This issue has been labeled inactive because it has been open 30 days with no activity. This will be closed in 7 days without further activity.
edit: No this will not be closed--sorry about that!
This issue has been labeled inactive because it has been open 180 days with no activity. Please consider closing this issue if no further action is needed.