Introducing Arex388.AspNet.Mvc.Startup NuGet Package to Simulate ASP.NET Core's Startup.cs in Classic ASP.NET MVC Applications
It's been a while since I wrote anything on here because I've been busy out of my mind, but I recently made something that I want to share with everyone.
At my work, our main application is an ASP.NET MVC 5 app (5.2.7 to be exact). I use Ninject for dependency injection and it has been working well for ~6 years so far. That being said, I was having a very hard time getting AutoMapper's instance API registered with Ninject. I tried the example in AutoMapper's docs, but I couldn't get it to work. Because of this, I couldn't update to AutoMapper 9 where the static API no longer exists.
After spending a few days reading and researching how to get AutoMapper and Ninject to work I ran across a blog post from Scott Dorman about using the Microsoft.Extensions.DependencyInjection NuGet package with classic ASP.NET MVC apps. I tried implementing his example, but couldn't get it to work at all either.
After several more days of research, I stumbled onto a Gist from David Fowler where he takes a slightly different approach. I decided to give it a shot and this time it worked! Finally, I had a working code sample. From there I just messed around with it in a throw-away app to get the hang of it. Once I was satisfied I decided to extract it out into its own library so I could share it on NuGet.
Originally it was called Arex388.AspNet.Mvc.DependencyInjection, but as I progressed through it, I realized that using Owin I can make it essentially simulate ASP.NET Core's Startup.cs
file, except it would all be in the Global.asax.cs
. So the current version is a mix of code examples from David Fowler and Scott Dorman with some sprinkles of code from me here and there to glue it together.
How to Use
- 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 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
}
}
}
Conclusion
As you can see it's very simple and easy, seems to "just work" and for me at least has helped me make sure my ~6 year old project is still keeping up with ASP.NET Core somehow.
And with that here's a delayed Christmas present to end off 2019! The source code is available on GitHub, and the package is available on NuGet.