-
Notifications
You must be signed in to change notification settings - Fork 2
Usage with MVC 5
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.
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
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");
});
}
}
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();
});In your RegisterRoutes method (usually in RouteConfig.cs in the App_Start folder), add the following line:
routes.UseGenericBackend();You have to use the backendconfig.json file for the config, as there is no appsettings.json in MVC 5
Make sure, you have <httpRuntime targetFramework="4.8" /> (or at least 4.5) in web.config, else you will get weird errors.