AxaFrance.Extensions.DependencyInjection.WebApi
1.3.1
Prefix Reserved
dotnet add package AxaFrance.Extensions.DependencyInjection.WebApi --version 1.3.1
NuGet\Install-Package AxaFrance.Extensions.DependencyInjection.WebApi -Version 1.3.1
<PackageReference Include="AxaFrance.Extensions.DependencyInjection.WebApi" Version="1.3.1" />
paket add AxaFrance.Extensions.DependencyInjection.WebApi --version 1.3.1
#r "nuget: AxaFrance.Extensions.DependencyInjection.WebApi, 1.3.1"
// Install AxaFrance.Extensions.DependencyInjection.WebApi as a Cake Addin #addin nuget:?package=AxaFrance.Extensions.DependencyInjection.WebApi&version=1.3.1 // Install AxaFrance.Extensions.DependencyInjection.WebApi as a Cake Tool #tool nuget:?package=AxaFrance.Extensions.DependencyInjection.WebApi&version=1.3.1
AxaFrance Dependency Injection
About
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.
Packages
Install-Package AxaFrance.Extensions.DependencyInjection.Mvc
Install-Package AxaFrance.Extensions.DependencyInjection.Owin
Install-Package AxaFrance.Extensions.DependencyInjection.WCF
Install-Package AxaFrance.Extensions.DependencyInjection.WebApi
Getting Started
Registering services
You can register your services like you would in ASP.NET Core:
services.AddScoped<IScopedService, ScopedService>()
.AddTransient<ITransientService, TransientService>()
.AddSingleton<ISingletonService, SingletonService>();
Using services
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
}
WCF
- Install the WCF package:
Install-Package AxaFrance.Extensions.DependencyInjection.WCF
- Create a class that inherits
DIServiceHostFactory
to 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
.svc
of 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.
OWIN
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.
WebApi & MVC
Install the WebApi package:
Install-Package AxaFrance.Extensions.DependencyInjection.WebApi
Install-Package AxaFrance.Extensions.DependencyInjection.Mvc
Register 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.
Dependencies
For all packages
- Microsoft.Extensions.DependencyInjection.Abstractions
For MVC
- Microsoft.AspNet.Mvc
For Owin
- Microsoft.Owin
- Owin
For WCF
- System.ServiceModel.Primitives
- Microsoft.Extensions.DependencyInjection
For WebApi
- Microsoft.AspNet.WebApi
Contributing
We welcome all contributions. Our contribution guidelines can be found here.
Acknowledgement
Thanks to our amazing contributors:
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.2
- Microsoft.AspNet.WebApi (>= 5.2.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.