[feature/netcore] ODataFormatter not using injected ODataSerializerProvider.
ODataFormatter not using injected ODataSerializerProvider when injecting ODataSerializerProvider on a per-route basis when either 2 routes or 2 servers exists in the same process.
Assemblies affected
7.0.0-Nightly201801081323
Reproduce steps
Run CutomizeSerializerProvider and any other E2E tests that causes formatting o occur in the same process (where the server is not isolated in an AppDomain).
Expected result
ODataFormatter uses the injected ODataSerializerProvider.
Actual result
ODataFormatter uses the default ODataSerializerProvider.
Additional detail
WebApi has a class called ODataSerializerProviderProxy which is used to delay-load the ODataSerializerProvider from the DI container. The proxy is used by the formatter, which is created outside of the DI container and therefore can’t use the DI container directly. At run-time, the DI container from the active request is saved on a property on the proxy before the formatter asks for the ODataSerializerProvider; once it does ask, the proxy loads the ODataSerializerProvider from DI. In most cases, all works great and as expected.
Currently, the formatters use a static instance of ODataSerializerProviderProxy: private static readonly ODataSerializerProviderProxy _instance = new ODataSerializerProviderProxy();
Formatters are a bit odd in AspNetClassic as they are not pre-route; they are injected into the configuration via a controller configuration attribute. However, we “support” DI of the ODataSerializerProvider on a per-route basis, i.e. we let you. So we don’t actually support ODataSerializerProvider on a per-route basis because there are only one set of formatters on the config. As to which ODataSerializerProvider is used, it’s a race condition as to which route gets a request and sets the DI container for the proxy; it could be either one.
The DI tests for ODataSerializerProvider exposes a separate issue where if two servers, i.e. 2 HttpConfig’s, exists in the same process (and each one has a set of formatters), the pre-route DI containers from one of the requests is used for both sets of formatters; the formatters in separate HTTPConfiguration’s share the static proxy instance and its associated per-route DI container (set via the race condition). In this case, even a configuration with a single route will not use the ODataSerializerProvider injected via DI. The more I think about this, it’s probably the less interesting case; customers would be more likely to run each server instance in it’s own process (like the tests used to do when the used AppDomains for each server).