dotnet / dotnet/aspnetcore

Initialize options of RequestLocalizationMiddleware in Invoke instead of constructor

Open
#60,780 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

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.

```cs
public class RequestLocalizationMiddleware
{
private readonly IOptions _options;

public RequestLocalizationMiddleware(RequestDelegate next, IOptions 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.