2015-12-20 17:09:46 +01:00
|
|
|
|
using System.Reflection;
|
2015-01-21 12:48:08 +11:00
|
|
|
|
using System.Web.Http.Controllers;
|
|
|
|
|
|
using System.Web.Mvc;
|
2015-12-20 17:09:46 +01:00
|
|
|
|
using LightInject;
|
2015-01-21 12:48:08 +11:00
|
|
|
|
using Umbraco.Core;
|
2016-05-18 23:34:56 +02:00
|
|
|
|
using Umbraco.Core.Plugins;
|
2015-01-21 12:48:08 +11:00
|
|
|
|
|
2015-12-20 17:09:46 +01:00
|
|
|
|
namespace Umbraco.Web
|
2015-01-21 12:48:08 +11:00
|
|
|
|
{
|
|
|
|
|
|
internal static class LightInjectExtensions
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// Registers all IControllers using the TypeLoader for scanning and caching found instances for the calling assembly
|
2015-01-21 12:48:08 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="container"></param>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// <param name="typeLoader"></param>
|
2016-07-04 11:10:38 +02:00
|
|
|
|
/// <param name="assembly"></param>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
public static void RegisterMvcControllers(this IServiceRegistry container, TypeLoader typeLoader, Assembly assembly)
|
2015-01-21 12:48:08 +11:00
|
|
|
|
{
|
2016-07-04 11:10:38 +02:00
|
|
|
|
//TODO: We've already scanned for UmbracoApiControllers and SurfaceControllers - should we scan again
|
2016-06-01 00:36:10 +02:00
|
|
|
|
// for all controllers? Seems like we should just do this once and then filter. That said here we are
|
|
|
|
|
|
// only scanning our own single assembly. Hrm.
|
|
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
container.RegisterControllers<IController>(typeLoader, assembly);
|
2015-01-21 12:48:08 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// Registers all IHttpController using the TypeLoader for scanning and caching found instances for the calling assembly
|
2015-01-21 12:48:08 +11:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="container"></param>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
/// <param name="typeLoader"></param>
|
2016-07-04 11:10:38 +02:00
|
|
|
|
/// <param name="assembly"></param>
|
2017-05-30 15:33:13 +02:00
|
|
|
|
public static void RegisterApiControllers(this IServiceRegistry container, TypeLoader typeLoader, Assembly assembly)
|
2015-01-21 12:48:08 +11:00
|
|
|
|
{
|
2016-07-04 11:10:38 +02:00
|
|
|
|
//TODO: We've already scanned for UmbracoApiControllers and SurfaceControllers - should we scan again
|
2016-06-01 00:36:10 +02:00
|
|
|
|
// for all controllers? Seems like we should just do this once and then filter. That said here we are
|
|
|
|
|
|
// only scanning our own single assembly. Hrm.
|
|
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
container.RegisterControllers<IHttpController>(typeLoader, assembly);
|
2016-07-04 11:10:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-30 15:33:13 +02:00
|
|
|
|
private static void RegisterControllers<TController>(this IServiceRegistry container, TypeLoader typeLoader, Assembly assembly)
|
2016-07-04 11:10:38 +02:00
|
|
|
|
{
|
2017-05-30 15:33:13 +02:00
|
|
|
|
var types = typeLoader.GetTypes<TController>(specificAssemblies: new[] { assembly });
|
2015-01-21 12:48:08 +11:00
|
|
|
|
foreach (var type in types)
|
|
|
|
|
|
container.Register(type, new PerRequestLifeTime());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|