GithubHelp home page GithubHelp logo

axafrance / extensions-dependency-injection Goto Github PK

View Code? Open in Web Editor NEW
18.0 7.0 6.0 120 KB

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

License: MIT License

C# 100.00%
dependency-injection dotnet package microsoft extensions guildevopen

extensions-dependency-injection's Introduction

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:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.