Open Closed

Getting Tenant in IActionFilter #872


User avatar
0
RonaldR created

Check the docs before asking a question: https://docs.abp.io/en/commercial/latest/ Check the samples, to see the basic tasks: https://docs.abp.io/en/commercial/latest/samples/index The exact solution to your question may have been answered before, please use the search on the homepage.

  • ABP Framework version: v4.1.2
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Seperated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:
  • We are using IActionFlters to put performance treacking in out api calls. we need to know the tenant information. i do not see how to get the tenant in there
  • In the WebModule we add this filter
  • context.Services.AddMvc(options => options.Filters.Add(new TrackPerformanceFilter(connStr, configuration)));
  • So it is not using DI, then we have data in the OnActionExecuting
public void OnActionExecuting(ActionExecutingContext context)
{
    var request = context.HttpContext.Request;

    string paramsList = string.Empty;
    foreach (var key in context.RouteData.Values?.Keys)
    {
        paramsList += $"{key}={(string)context.RouteData.Values[key]},";
    }

    if (string.IsNullOrEmpty(paramsList) == false)
    {
        paramsList = paramsList[0..^1];
    }

    PerformanceTrackingModel trackingInfo = new PerformanceTrackingModel
    {
        TenantId = Guid.NewGuid(),
        StartedDateTime = DateTime.UtcNow,
        ProcessName = request.Host.Host,
        UrlPath = request.Path,
        UrlMethod = request.Method,
        UrlParameters = paramsList
    };

    _tracker.StartTracking(trackingInfo);
}

But i do not see how i can get the tenant in this function


3 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    You can inject the ICurrentTenant service to get current tenant id.

    https://github.com/abpframework/abp/blob/dev/framework/src/Volo.Abp.MultiTenancy/Volo/Abp/MultiTenancy/ICurrentTenant.cs#L11

  • User Avatar
    0
    RonaldR created

    no, we cant. in the web module, which only runs on startup public override void ConfigureServices(ServiceConfigurationContext context)

    we create the filter and add it to the filter stack, as stated above: context.Services.AddMvc(options => options.Filters.Add(new TrackPerformanceFilter(connStr, configuration)));

    so when the class is instastiated, it is start up, not at the time the controller is invoked, so we dont know the tenant, plus, it is only created once, there is no option to do DI in to the filter

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    You can get service via HttpContext.RequestServices

    var currentTenant = context.HttpContext.RequestServices.GetRequiredService<ICurrentTenant>();
    
Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11