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.
- David Fowler's Gist example code.
- Scott Dorman's Blog example code.
- The StackOverflow question that lead me to David and Scott.
I also want to thank Keith Dahlby for helping improve the library.
- Add the Arex388.AspNet.Mvc.Startup NuGet package.
- Change your
Global.asax.cs
to inherit fromStartupApplication
. - Add
[assembly: OwinStartup(typeof(YourNamespace.MvcApplication))]
attribute to the namespace of yourGlobal.asax.cs
. - Implement the
Configure
andConfigureServices
methods inherited fromStartupApplication
. - Add
ConfigureServices()
to the end ofApplication_Start
.
Here's a more complete example:
[assembly: OwinStartup(typeof(YourNamespace.MvcApplication))]
namespace YourNamespace {
public class MvcApplication :
StartupApplication {
public override void 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
}
}
}
- Added
GetServiceProvider()
extension onIAppBuilder
. This extension provides access to theIServiceProvider
when working with services in theConfigure()
method. - Reverted back all dependencies on
Microsoft.Extensions.DependencyInjection
to v2.1.1. You can continue to use the latest (v5.0.2 as of this writing) with binding redirects.
The only annoyance I've had so far is with Hangfire's Using IServiceCollection
extensions because they're in the Hangfire.AspNetCore
NuGet package and that forces 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.Hangfire.NetCore
package instead simplified this issue.