Skip to content

Usage with MVC 5

Adrian Ehrsam edited this page Jan 27, 2021 · 3 revisions

Usage with .Net Framework 4.8 and MVC 5

Install Packages

Install the following packages:

  • Kull.GenericBackend
  • Swashbuckle.Core
  • Unity.Mvc

This should create a UnityMvcActivator.cs and a UnityConfig.cs in the App_Start directory.

If you want to use Unity in your Web Api part as well, add the following package, too:

  • Unity.AspNet.WebApi

This will ask you if you want to override UnityConfig.cs. Do not do this.

UnityMvcActivator.cs

There you'll find something like this:

// TODO: Uncomment if you want to use PerRequestLifetimeManager
//Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

Do as it is written and uncomment this line, as we rely on the PerRequestLifetimeManager

UnityConfig.cs

You only have to place code in UnityConfig.cs usually. It should contains something like this:

using Kull.DatabaseMetadata;
using Kull.GenericBackend;
using Kull.MvcCompat;
using Newtonsoft.Json.Serialization;
using System.Data.Common;
using Unity;


 public class UnityConfig
    {
        public static UnityContainer Container { get; } = new UnityContainer();

        public static void RegisterServices()
        {
            Container.AddMvcCompat();
            Container.AddKullDatabaseMetadata();
            Container.AddGenericBackend()
                .ConfigureMiddleware(m =>
                {
                    // In MVC 5 it usually was common to use PascalCase. DefaultNamingStrategy just leaves everything as is
                    m.NamingStrategy = new DefaultNamingStrategy();
                })
                .AddSystemParameters()
                .AddFileSupport();
            Container.AddTransient<DbConnection>((sp) =>
            {
                return Kull.Data.DatabaseUtils.GetConnectionFromConfig("the config name");
            });
        }
    }
	

Global.asax

In the global file, you have to add the Config for swagger:

GlobalConfiguration.Configure(http =>
            {
                //Swagger is added for testing purposes 
                // http://localhost:59150/swagger/docs/v1
                // http://localhost:59150/swagger/ui/index
                http.EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Api v1");
                    c.PrettyPrint();
                    c.AddGenericBackend(); // IMPORTANT!
                })
                // If you want, enable swagger ui
                //.EnableSwaggerUi();

            });

RouteConfig

In your RegisterRoutes method (usually in RouteConfig.cs in the App_Start folder), add the following line:

routes.UseGenericBackend();

backendconfig.json

You have to use the backendconfig.json file for the config, as there is no appsettings.json in MVC 5

Web.config

Make sure, you have <httpRuntime targetFramework="4.8" /> (or at least 4.5) in web.config, else you will get weird errors.

Clone this wiki locally