Skip to content

Microsoft Extensions DependencyInjection for WCF, and asp.net classic projects

License

Notifications You must be signed in to change notification settings

AxaFrance/extensions-dependency-injection

Repository files navigation

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: