GitXplorerGitXplorer
R

NBS.Core.Web.Mvc.DependencyInjection

public
1 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
feadee083c21d15f1a86b3409de3eba0f855be34

Update README.md

RRichardD2 committed 2 years ago
Unverified
4f7107108e2712148860f97ea351c288c8907fa6

Add project files.

RRichardD2 committed 2 years ago
Unverified
f8ff9b48e4d707448c9e76ecc8c5003bde3f76fe

Add .gitattributes, .gitignore, README.md, and LICENSE.txt.

RRichardD2 committed 2 years ago

README

The README file for this repository.

NBS.Core.Web.Mvc.DependencyInjection

Dependency injection for ASP.NET MVC 5 applications using the Microsoft DI abstractions library.

Usage

  1. Add a reference to the Microsoft.Extensions.DependencyInjection NuGet package.
  2. Add a reference to this library.
  3. Build your service provider.
  4. Use this library to register your service provider as the IDependencyResolver for the application.

Example

using System;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using System.Web.Routing;
using Microsoft.Extensions.DependencyInjection;
using NBS.Core.Web.Mvc.DependencyInjection;
using Scrutor;

static class DependencyConfig
{
    public static void Register()
    {
        IServiceCollection services = new ServiceCollection();
        ConfigureServices(services);
        IServiceProvider provider = services.BuildServiceProvider(true);
        provider.RegisterMvcDependencyInjection();
    }
    
    private static void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(RouteTable.Routes);
        services.AddScoped(_ => HttpContext.Current);
        services.AddScoped<HttpContextBase>(sp => new HttpContextWrapper(sp.GetRequiredService<HttpContext>()));
        services.AddScoped(sp => sp.GetRequiredService<HttpContextBase>().Request);
        services.AddScoped(sp => sp.GetRequiredService<RouteCollection>().GetRouteData(sp.GetRequiredService<HttpContextBase>()));
        services.AddScoped(sp => new RequestContext(sp.GetRequiredService<HttpContextBase>(), sp.GetRequiredService<RouteData>()));
        services.AddScoped(sp => new UrlHelper(sp.GetRequiredService<RequestContext>(), sp.GetRequiredService<RouteCollection>()));
        
        // Use Scrutor to register all controllers as scoped services:
        services.Scan(scan => scan.FromAssemblyOf<MvcApplication>()
            .AddClasses(classes => classes.AssignableTo<Controller>())
            .AsSelf().WithScopedLifetime());
    }
}