Suggestions please - upgraded web api Odata to v6, how to delegate service registrations to autofac?

175 views
Skip to first unread message

katri...@gmail.com

unread,
Mar 24, 2017, 6:28:42 AM3/24/17
to Autofac
We are using .net framework not .net core for our odata web services and we were used to calling autofac dependency resolver to register dependencies. However after upgrading to odata v6(http://odata.github.io/WebApi/#13-04-DependencyInjection), we'll have to create ContainerBuilder and IServiceProvider to delegate all the service registrations to autofac.

I know autofac has released its own Extensions.DepndencyInjection following Microsoft.Extensions.DependencyInjection, but we cannot migrate to .net core for now. I've implemented IServiceProvider but got stuck on ContainerBuilder, how would you suggestion to wrap these functions?
e.g.
IContainerBuilder AddService(
ServiceLifetime lifetime,
Type serviceType,
Func<IServiceProvider, object> implementationFactory);

Thanks,
...Katrina

Travis Illig

unread,
Mar 24, 2017, 12:32:29 PM3/24/17
to Autofac, katri...@gmail.com
Microsoft.Extensions.DependencyInjection is compatible with both .NET Core and full .NET framework. You shouldn't have to wrap anything. Have you tried it? Have you run into a problem?

katri...@gmail.com

unread,
Mar 24, 2017, 1:52:21 PM3/24/17
to Autofac, katri...@gmail.com
On Friday, March 24, 2017 at 9:32:29 AM UTC-7, Travis Illig wrote:
> Microsoft.Extensions.DependencyInjection is compatible with both .NET Core and full .NET framework. You shouldn't have to wrap anything. Have you tried it? Have you run into a problem?

I know Microsoft.Extensions.DependencyInjection is compatible with .net framework, but I am not sure about autofac. Using Microsoft.Extensions.DependencyInjection, I think I still need to delegate all the services to autofac according to odata DI doc (if you want to use a different DI framework (e.g., Autofac) or make some customizations to the default behavior, you will need to either implement your own container builder from IContainerBuilder or inherit from the DefaultContainerBuilder). Or you suggested that we may use Microsoft.Extensions.DependencyInjection rather than autofac?


Travis Illig

unread,
Mar 24, 2017, 2:01:56 PM3/24/17
to Autofac, katri...@gmail.com
I think it would help you to just try it. Autofac, Autofac.Extensions.DependencyInjection, and Microsoft.Extensions.DependencyInjection are all compatible with both full .NET and .NET Core. You shouldn't have to do anything special.

You can see how to set up Autofac as the IServiceProvider by using Autofac.Extensions.DependencyInjection by looking at our ASP.NET Core documentation. But don't be confused! The example/doc is showing ASP.NET Core because that's the majority use case. If you need an IServiceProvider that is Autofac-backed, you can use Autofac.Extensions.DependencyInjection in conjunction with Microsoft.Extensions.DependencyInjection and that should get you what you need for your OData 6 stuff. We have our own IServiceProvider implementation that is fully compatible already. Register your stuff in Autofac or with the Microsoft.Extensions.DependencyInjection stuff, populate any M.E.DI registrations into your container, and use the Autofac service provider.

If you run into a specific problem using Autofac + Autofac.Extensions.DependencyInjection + Microsoft.Extensions.DependencyInjection in combination, let us know. But there's nothing to wrap, there's nothing we can "suggest" - it's already set to go for you. Just hook it up.

If I'm not answering your question, please clarify what isn't working: What have you tried? What isn't working? Is there an error? Is something not compiling?
Reply all
Reply to author
Forward
0 new messages