添加到集合后如何配置服务选项?

How to configure service options after it has been added to the collection?

有没有办法在服务(在本例中为 AddMvc)添加到服务集合后为其配置选项?这是我需要的示例:

正常添加服务:

services.AddMvc(opt =>
{
    ...
});

然后,稍后在代码中,update\add 已经添加的服务的一些选项。

services.AddMvc().AddJsonOptions(opt =>
{
    ...
});

这是一个 API 管道,使用 .NET Core 2.2 构建。

AddJsonOptions 的调用添加了一个配置委托,稍后当 MvcJsonOptions 的实例实际上是 built/configured 时调用它。您稍后可以通过在 IServiceCollection 本身上添加对 Configure<T> 的调用来实现相同的结果:

services.AddMvc(opt =>
{
    // ...
});

// ...

services.Configure<MvcJsonOptions>(opt =>
{
    // ...
});

参考:Configure simple options with a delegate.