user2146402
user2146402

Reputation: 51

OData - DefaultODataSerializerProvider constructor added

Just upgraded from Microsoft.AspNet.OData 5.9.0 to 6.0.0.

The DefaultODataSerializerProvider class now has a constructor argument IServiceProvider.

I had a class NullSerializerProvider that implemented DefaultODataSerializerProvider.

This was adding a formatter to HttpConfiguration. e.g.

public static void Register(HttpConfiguration config)
{
    ...
    config.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create(new 
    NullSerializerProvider(), new DefaultODataDeserializerProvider()));
}

My question: NullSerializerProvider expects an instance of a class that implements IServiceProvider to be supplied. How do I supply this?

Upvotes: 2

Views: 909

Answers (2)

Jamaxack
Jamaxack

Reputation: 2460

I also faced with this issue and after day of research find this solution:

using Microsoft.Extensions.DependencyInjection;
var serviceProvider = new ServiceCollection().BuildServiceProvider() as IServiceProvider;

Now you can create your own SerializerProvider or DeserializerProvider and use like:

 var odataFormatters = ODataMediaTypeFormatters.Create(new DefaultODataSerializerProvider(serviceProvider), new JsonODataDeserializerProvider(serviceProvider));
 config.Formatters.AddRange(odataFormatters);

Hope it save a lot of time and headache.

Upvotes: 0

Pete Craig
Pete Craig

Reputation: 11

Here is the documentation on the OData dependency injection: http://odata.github.io/odata.net/v7/#01-05-di-support

http://odata.github.io/WebApi/#13-04-DependencyInjection

I spent the whole day pouring over this information and trying to create my own IContainerBuilder around Ninject with no luck.

I finally gave up and let OData use its own DI container and keep that separate from my use of Ninject. I then realized I could create the DefaultContainerBuilder and register it like a custom container builder. I could then use the DefaultContainerBuilder to get the IServiceProvider.

        var containerBuilder = new DefaultContainerBuilder(); 
        config.UseCustomContainerBuilder(() => containerBuilder);
        config.EnableDependencyInjection();
        var serviceProvider = containerBuilder.BuildContainer();
        var oDataSerializerProvider = new DefaultODataSerializerProvider(serviceProvider);

Upvotes: 1

Related Questions