AxaFrance.Extensions.DependencyInjection.Owin 1.3.1

Prefix Reserved
dotnet add package AxaFrance.Extensions.DependencyInjection.Owin --version 1.3.1                
NuGet\Install-Package AxaFrance.Extensions.DependencyInjection.Owin -Version 1.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AxaFrance.Extensions.DependencyInjection.Owin" Version="1.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AxaFrance.Extensions.DependencyInjection.Owin --version 1.3.1                
#r "nuget: AxaFrance.Extensions.DependencyInjection.Owin, 1.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AxaFrance.Extensions.DependencyInjection.Owin as a Cake Addin
#addin nuget:?package=AxaFrance.Extensions.DependencyInjection.Owin&version=1.3.1

// Install AxaFrance.Extensions.DependencyInjection.Owin as a Cake Tool
#tool nuget:?package=AxaFrance.Extensions.DependencyInjection.Owin&version=1.3.1                

AxaFrance Dependency Injection

Continuous Integration Quality Gate Reliability Security Code Corevage

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

  1. Install the WCF package:
Install-Package AxaFrance.Extensions.DependencyInjection.WCF
  1. 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>();
    }
}
  1. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.1 775 6/30/2023
1.3.0 598 9/14/2022
1.2.0 1,051 1/29/2021
1.1.0 465 9/17/2020
1.0.0 995 10/3/2019
0.1.0-beta0001 382 9/26/2019