Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. "Message":"An error has occurred.","ExceptionMessage":"An error occurred
  2. when trying to create a controller of type 'MyController'. Make sure that the
  3. controller has a parameterless public
  4. constructor.","ExceptionType":"System.InvalidOperationException"
  5.  
  6. public class MyController : ApiController
  7. {
  8. private IMyManager MyManager { get; set; }
  9.  
  10. public MyController(IMyManager myManager)
  11. {
  12. this.MyManager = myManager;
  13. }
  14.  
  15. ...
  16. }
  17.  
  18. container.RegisterType<IMyManager, MyManager>();
  19.  
  20. {"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)
  21. at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
  22. 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)
  23. at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
  24. at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
  25. at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}
  26.  
  27. using Microsoft.Practices.Unity;
  28. using System.Web.Http;
  29. using System.Web.Mvc;
  30.  
  31. namespace WebApplication1
  32. {
  33. public static class UnityConfig
  34. {
  35. public static void RegisterComponents()
  36. {
  37. var container = new UnityContainer();
  38.  
  39. // register all your components with the container here
  40. // it is NOT necessary to register your controllers
  41.  
  42. // e.g. container.RegisterType<ITestService, TestService>();
  43.  
  44. // Configures container for ASP.NET MVC
  45. DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
  46.  
  47. // Configures container for WebAPI
  48. GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
  49. }
  50. }
  51. }
  52.  
  53. old.managers.IMyManager
  54.  
  55. new.managers.IMyManager
  56.  
  57. container.RegisterType<IMyManager, MyManager>();
  58.  
  59. public MyController() {}
  60.  
  61. public static void RegisterComponents()
  62. {
  63. var container = new UnityContainer();
  64.  
  65. container.RegisterType<IMyManager, MyManager>();
  66.  
  67. MvcUnityContainer.Container = container;
  68.  
  69. DependencyResolver.SetResolver(new UnityDependencyResolver(container));
  70. }
  71.  
  72. public class UnityControllerFactory : DefaultControllerFactory
  73. {
  74. public override IController CreateController(RequestContext requestContext, string controllerName)
  75. {
  76. Type controllerType = null;
  77. if (TypeHelper.LooksLikeTypeName(controllerName))
  78. {
  79. controllerType = TypeHelper.GetType(controllerName);
  80. }
  81.  
  82. if (controllerType == null)
  83. {
  84. controllerType = this.GetControllerType(requestContext, controllerName);
  85. }
  86.  
  87. return controllerType != null ? this.GetControllerInstance(requestContext, controllerType) : null;
  88. }
  89.  
  90. protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
  91. {
  92. try
  93. {
  94. if (controllerType == null)
  95. throw new ArgumentNullException("controllerType");
  96.  
  97. if (!typeof(IController).IsAssignableFrom(controllerType))
  98. throw new ArgumentException(string.Format(
  99. "Type requested is not a controller: {0}",
  100. controllerType.Name),
  101. "controllerType");
  102.  
  103. return MvcUnityContainer.Container.Resolve(controllerType) as IController;
  104. }
  105. catch
  106. {
  107. return null;
  108. }
  109. }
  110. }
  111.  
  112. public static class MvcUnityContainer
  113. {
  114. public static IUnityContainer Container { get; set; }
  115. }
  116.  
  117. UnityConfig.RegisterComponents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement