Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "Message":"An error has occurred.","ExceptionMessage":"An error occurred
- when trying to create a controller of type 'MyController'. Make sure that the
- controller has a parameterless public
- constructor.","ExceptionType":"System.InvalidOperationException"
- public class MyController : ApiController
- {
- private IMyManager MyManager { get; set; }
- public MyController(IMyManager myManager)
- {
- this.MyManager = myManager;
- }
- ...
- }
- container.RegisterType<IMyManager, MyManager>();
- {"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
- at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
- at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Type 'MyProject.Web.Api.Controllers.MyController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)
- at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
- at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
- at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}
- using Microsoft.Practices.Unity;
- using System.Web.Http;
- using System.Web.Mvc;
- namespace WebApplication1
- {
- public static class UnityConfig
- {
- public static void RegisterComponents()
- {
- var container = new UnityContainer();
- // register all your components with the container here
- // it is NOT necessary to register your controllers
- // e.g. container.RegisterType<ITestService, TestService>();
- // Configures container for ASP.NET MVC
- DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
- // Configures container for WebAPI
- GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
- }
- }
- }
- old.managers.IMyManager
- new.managers.IMyManager
- container.RegisterType<IMyManager, MyManager>();
- public MyController() {}
- public static void RegisterComponents()
- {
- var container = new UnityContainer();
- container.RegisterType<IMyManager, MyManager>();
- MvcUnityContainer.Container = container;
- DependencyResolver.SetResolver(new UnityDependencyResolver(container));
- }
- public class UnityControllerFactory : DefaultControllerFactory
- {
- public override IController CreateController(RequestContext requestContext, string controllerName)
- {
- Type controllerType = null;
- if (TypeHelper.LooksLikeTypeName(controllerName))
- {
- controllerType = TypeHelper.GetType(controllerName);
- }
- if (controllerType == null)
- {
- controllerType = this.GetControllerType(requestContext, controllerName);
- }
- return controllerType != null ? this.GetControllerInstance(requestContext, controllerType) : null;
- }
- protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
- {
- try
- {
- if (controllerType == null)
- throw new ArgumentNullException("controllerType");
- if (!typeof(IController).IsAssignableFrom(controllerType))
- throw new ArgumentException(string.Format(
- "Type requested is not a controller: {0}",
- controllerType.Name),
- "controllerType");
- return MvcUnityContainer.Container.Resolve(controllerType) as IController;
- }
- catch
- {
- return null;
- }
- }
- }
- public static class MvcUnityContainer
- {
- public static IUnityContainer Container { get; set; }
- }
- UnityConfig.RegisterComponents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement