简单注入器 - 确保控制器在生产时具有无参数的公共构造函数

时间:2015-08-23 16:33:55

标签: c# asp.net angularjs dependency-injection simple-injector

我陷入了一种奇怪的境地。我关注Onion Architecture,我的架构是这样的:

1-Core
     - Domain Classes
     - Repository Interfaces
     - Service Interfaces
2-Infrastructure
     - Data
     - Dependency Injection // Here I am using Simple Injector as dependency injection
     - Repository Interfaces Implementation
     - Service Interfaces Implementation
3-WebApi
     - Web Api Project
4-WebClient
     - My AngularJs App
5-Test
     - Test Project

依赖注入:

[assembly: PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace Infrastructure.DependencyResolution
{
    public class IocConfig
    {
        public static void RegisterDependencies()
        {
            var container = new Container();

            container.RegisterWebApiRequest<IRepositoryAsync<Category>, Repository<Category>>();
            container.RegisterWebApiRequest<ICategoryService, CategoryService>();
            container.RegisterWebApiRequest<IDataContextAsync>(() => new MyContext());
            container.Verify();
            GlobalConfiguration.Configuration.DependencyResolver =
            new SimpleInjectorWebApiDependencyResolver(container);

        }
    }
}

Web Api项目:

public class HomeController : ApiController
{
    private readonly ICategoryService _categoryService;
    public HomeController(ICategoryService categoryService)
    {
        _categoryService = categoryService;
    }
}

local IIS上的一切都很好。但是现在我已经将这个应用程序发布到了生产服务器,现在它给了我以下错误:

  

{“message”:“发生错误。”,“exceptionMessage”:“错误   尝试创建'HomeController'类型的控制器时发生。   确保控制器具有无参数公共   构造函数 “” exceptionType。 “:” System.InvalidOperationException “ ”堆栈跟踪“:”   在   System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage   请求,HttpControllerDescriptor controllerDescriptor,Type   controllerType)\ r \ n at   System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage   请求)\ r \ n at   System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext() “ ”的InnerException“:{ ”消息“:” 一种   错误已经发生。“,”exceptionMessage“:”类型   'JetAdz.WebApi.Controllers.HomeController'没有默认值   构造函数 “ ”exceptionType“: ”System.ArgumentException“, ”堆栈跟踪“:”   在System.Linq.Expressions.Expression.New(Type type)\ r \ n at   System.Web.Http.Internal.TypeActivator.Create [TBASE](类型   instanceType)\ r \ n at   System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage   请求,输入controllerType,Func`1&amp;激活者)\ r \ n at   System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage   请求,HttpControllerDescriptor controllerDescriptor,Type   controllerType)“}}

2 个答案:

答案 0 :(得分:0)

我使用spring XML文件将变量注入到我的类中。在我的情况下,当我创建一个新的控制器时,它是一个复制/粘贴错误。

因此,虽然这与OP问题并不完全相关,但它会收到相同的错误消息,并帮助其他人。

我复制了(尖括号改为方括号):

db.myCollection.find({city: "new york"}).collation({locale: "en", strength: 2});

当我需要的是:

[object id="className" type="Project.ClassName" scope="request"]
    [constructor-arg name="OtherClassName" ref="otherclassName"/]
[/object]

注意属性而不是 constructor-arg

因为我的班级没有构造函数来查找参数。

答案 1 :(得分:0)

对我来说,只是因为我忘记将SimpleInjectorInitializer.Initialize();添加到我的Application_Start()中。

这几天我通常会在SimpleInjectorInitializer文件夹中创建一个App_Start类,在其中我进行container.Verify()GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);调用;在调用我的GetInitializeContainer()方法之后,该方法正在执行所有类型注册等。

相关问题