This package allows projects running on older version of .NET Framework to use the new dependency injection framework introduced with ASP.NET Core. It works with OWIN, MVC and WebApi web applications as well as WCF services.
This gives an easier migration path to those projects towards ASP.NET Core by allowing you to write your Injection configuration as if you were on ASP.NET Core.
Install-Package AxaFrance.Extensions.DependencyInjection.MvcInstall-Package AxaFrance.Extensions.DependencyInjection.OwinInstall-Package AxaFrance.Extensions.DependencyInjection.WCFInstall-Package AxaFrance.Extensions.DependencyInjection.WebApiYou can register your services like you would in ASP.NET Core:
services.AddScoped<IScopedService, ScopedService>()
.AddTransient<ITransientService, TransientService>()
.AddSingleton<ISingletonService, SingletonService>();Your services registered can now be passed to constructors:
public HomeController(ISingletonService singletonService, IServiceProvider serviceProvider)
{
this.singletonService = singletonService;
this.serviceProvider = serviceProvider;
this.transientServices = Enumerable.Range(0, 10)
.Select(_ => this.serviceProvider.GetService<ITransientService>());
}The IServiceProvider interface can be used to programmatically get service at runtime.
In WebApi/Mvc you can even use the [FromService] attribute in controller actions:
public ActionResult Index([FromServices] IScopedService scopedService)
{
//Logic
}- Install the WCF package:
Install-Package AxaFrance.Extensions.DependencyInjection.WCF- Create a class that inherits
DIServiceHostFactoryto register your services:
public class WithDependencyInjectionServiceFactory : DIServiceHostFactory
{
protected override void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IDataProvider, SampleDataProvider>();
services.AddTransient<ISampleService, SampleService>();
}
}- Declare this class as the factory in the
.svcof your webservice:
<%@ ServiceHost Language="C#" Debug="true" Service="AxaFrance.Extensions.DependencyInjection.WCF.Sample.SampleService" Factory="AxaFrance.Extensions.DependencyInjection.WCF.Sample.WithDependencyInjectionServiceFactory" %>A sample app is available.
In an OWIN application, you must register a startup class using the [OwinStartup] attribute:
[assembly: OwinStartup(typeof(Owin.Sample.Startup))]
namespace Owin.Sample
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
ServiceCollection services = new ServiceCollection();
services.AddScoped<IScopedService, ScopedService>()
.AddTransient<ITransientService, TransientService>()
.AddSingleton<ISingletonService, SingletonService>();
// Register the service provider
app.UseScopedServiceProvider(services.BuildServiceProvider());
//...
}
}
}A sample is also available.
Install the WebApi package:
Install-Package AxaFrance.Extensions.DependencyInjection.WebApi
Install-Package AxaFrance.Extensions.DependencyInjection.MvcRegister your service collection in Global.asax.cs:
IServiceProvider provider = new ServiceCollection()
.AddScoped<IScopedService, ScopedService>()
.AddSingleton<ISingletonService, SingletonService>()
.AddTransient<ITransientService, TransientService>()
.AddWebApi()
.AddMvc()
.BuildServiceProvider();
System.Web.Mvc.DependencyResolver.SetResolver(new Mvc.DefaultDependencyResolver(provider));
GlobalConfiguration.Configure(config => {
// ...
config.DependencyResolver = new DefaultDependencyResolver(provider);
// ...
});You need to configure the resolver for both MVC and WebApi if you use both in your application
A sample is also available.
- Microsoft.Extensions.DependencyInjection.Abstractions
- Microsoft.AspNet.Mvc
- Microsoft.Owin
- Owin
- System.ServiceModel.Primitives
- Microsoft.Extensions.DependencyInjection
- Microsoft.AspNet.WebApi
We welcome all contributions. Our contribution guidelines can be found here.
Thanks to our amazing contributors: