Skip to content

Initialize options of RequestLocalizationMiddleware in Invoke instead of constructor #60780

Open
@Sneedd

Description

@Sneedd

Could you change the initialization of the options from the constructor into Invoke()?

The reason behind is that in the property Value the Options is initialized inside the constructor, but if the property access would be done in Invoke() the initialization will be during a request.

public class RequestLocalizationMiddleware
{
    private readonly IOptions<RequestLocalizationOptions> _options;
  
    public RequestLocalizationMiddleware(RequestDelegate next, IOptions<RequestLocalizationOptions> options)
    {
        _options = options;
    }

    public async Task Invoke(HttpContext context)
    {
        var options = _options.Value;  // Now the value is created here instead of the constructor
    }
}

See

In my case I load the options from a database and I do not want to do it during startup ... and I cannot change that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions