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

Query returns no results and NullReferenceException #920

Open
Hulkstance opened this issue Dec 21, 2024 · 3 comments
Open

Query returns no results and NullReferenceException #920

Hulkstance opened this issue Dec 21, 2024 · 3 comments
Labels

Comments

@Hulkstance
Copy link

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:

image

image


Issue 2: NullReferenceException for Invalid Tenant

When an invalid tenant identifier (e.g. asd) is passed, the application throws a NullReferenceException.

Screenshot:

image


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

@funkel1989
Copy link

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.

@AndrewTriesToCode
Copy link
Contributor

@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

@Hulkstance
Copy link
Author

Hulkstance commented Dec 31, 2024

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.

image

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

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

3 participants