How to overwrite a scoped service with a decorated implementation?

后端 未结 5 600
日久生厌
日久生厌 2020-12-18 06:07

I\'m trying to write an ASP.NET Core 2.2 integration test, where the test setup decorates a specific service that would normally be available to the API as a dependency. The

5条回答
  •  爱一瞬间的悲伤
    2020-12-18 06:51

    This seems like a limitation of the servicesConfiguration.AddXxx method which will first remove the type from the IServiceProvider passed to the lambda.

    You can verify this by changing servicesConfiguration.AddScoped(...) to servicesConfiguration.TryAddScoped(...) and you'll see that the original BarService.GetValue is getting called during the test.

    Additionally, you can verify this because you can resolve any other service inside the lambda except the one you're about to create/override. This is probably to avoid weird recursive resolve loops which would lead to a stack-overflow.

提交回复
热议问题