Skip to content
ASP.NET MVC "classic" dependency injection and service configuration extensions to simulate ASP.NET Core's Startup configuration
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Extensions Initial commit Jan 3, 2020
Properties Initial commit Jan 3, 2020
.gitattributes Initial commit Jan 3, 2020
.gitignore
Arex388.AspNet.Mvc.Startup.csproj Initial commit Jan 3, 2020
Arex388.AspNet.Mvc.Startup.nuspec Initial commit Jan 3, 2020
Constants.cs Initial commit Jan 3, 2020
README.md Initial commit Jan 3, 2020
ServiceProviderDependencyResolver.cs Initial commit Jan 3, 2020
ServiceScopeModule.cs Initial commit Jan 3, 2020
StartupApplication.cs Initial commit Jan 3, 2020
app.config Initial commit Jan 3, 2020
packages.config Initial commit Jan 3, 2020

README.md

Arex388.AspNet.Mvc.Startup

This is a classic ASP.NET MVC helper library that started out as a way for me to use the Microsoft.Extensions.DependencyInjection NuGet package for dependency injection. As I worked on it, it dawned on me that with a little bit more nudging it can simulate ASP.NET Core's Startup.cs.

For a more detailed story about why this library came to be, please read my corresponding blog post here. I want to thank David Fowler and Scott Dorman for their code examples which I've taken, adapted, and mostly just glued together.

How to Use

  1. Add the Arex388.AspNet.Mvc.Startup NuGet package.
  2. Change your Global.asax.cs to inherit from StartupApplication.
  3. Add [assembly: OwinStartup(typeof(YourNamespace.MvcApplication))] attribute to the namespace of your Global.asax.cs.
  4. Implement the Configure and ConfigureServices methods inherited from StartupApplication.
  5. Add ConfigureServices() to the end of Application_Start.

Here's a more complete example:

[assembly: OwinStartup(typeof(YourNamespace.MvcApplication))]
namespace YourNamespace {
    public class MvcApplication :
        StartupApplication {
        public void Application_Start() {
            //	Other setup and configuration code here...

            ConfigureServices();
        }

		public override Configure(
			IAppBuilder app) {
			//	Add IAppBuilder configurations
        }

        public override void ConfigureServices(
            IServiceCollection services) {
            var assembly = typeof(MvcApplication).Assembly;

            //	Add your controllers
            services.AddControllers(assembly);

            //	Add other services that have IServiceCollection extensions
        }
    }
}

Things to Consider

The only annoyance I've had so far is with HangFire's IServiceCollection extensions because they're in the HangFire.AspNetCore NuGet package and that force's a bunch of other NuGet packages to be added in. I can live with it for the simplicity in configuration and dependency registrations I get otherwise.

You can’t perform that action at this time.