简体   繁体   English

如何解决Unity DI容器的随机错误:“确保控制器具有无参数的公共构造函数。”

[英]How to resolve random Unity DI Container error: “Make sure that the controller has a parameterless public constructor.”

Please note that the issue I'm experiencing is NOT a generic "IndexOutOfRangeException", which I am well familiar with. 请注意,我遇到的问题不是我很熟悉的通用“ IndexOutOfRangeException”。 The issue I am seeing is specific to the Unity DI container when using with an ASP.NET Web API back end, and occurs at random times. 我看到的问题是与ASP.NET Web API后端一起使用时特定于Unity DI容器的,并且是随机发生的。

I am using a Unity DI container within the context of an ASP.NET Web API back end written in C#. 我在用C#编写的ASP.NET Web API后端的上下文中使用Unity DI容器。 For the most part, the site works fine. 在大多数情况下,该网站都能正常运行。 However, at random times throughout the day, we are getting an HTTP 500 error code with the message "An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor. .... Exception is: IndexOutOfRangeException - Index was outside the bounds of the array". 但是,在一整天的随机时间内,我们会收到一条HTTP 500错误代码,并显示消息“尝试创建类型为'MyController'的控制器时发生错误。请确保该控制器具有无参数的公共构造函数。... 。异常是:IndexOutOfRangeException-索引超出了数组的界限。

We've tried using IIS configuration to recycle the app pool on a regular basis, but we are still getting this error. 我们尝试使用IIS配置定期回收应用程序池,但仍然会收到此错误。

Here's my code 这是我的代码

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    GlobalConfiguration.Configuration.EnableDependencyInjection();
    AreaRegistration.RegisterAllAreas();
    RouteConfig2.RegisterRoutes(RouteTable.Routes);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    UnityConfig.RegisterComponents();
}

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = UnityDI.Containers.GetContainer();
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}


namespace UnityDI
{
    public static class Containers
    {
        public static UnityContainer GetContainer(bool staticDbFactory = false)
        {
            var container = new UnityContainer();

            //DbContext
            if (staticDbFactory)
                container.RegisterType<IDbContextFactory, DbContextFactory>(new ContainerControlledLifetimeManager());
            else
                container.RegisterType<IDbContextFactory, DbContextFactory>();

            //Services
            container.RegisterType<IAddressService, AddressService>();
            container.RegisterType<IAdminService, AdminService>();
            /* (other items omitted for brevity) */

            /* EdiFileProcessorBase registrations */
            container.RegisterType<EdiFileProcessorBase, AbcInboundBolFileProcessor>(EdiFileTypeEnum.AbcInboundBol.ToString());
            container.RegisterType<EdiFileProcessorBase, AbcInboundVesselStatusFileProcessor>(EdiFileTypeEnum.AbcInboundVesselStatus.ToString());
            return container;
        }
    }
}

[InjectionConstructor]
public DocumentService(
    IDbContextFactory dbContextFactory,
    IBlobStorageService blobStorageService,
    Lazy<IEmailService> emailService,
    Lazy<ILocationService> locationService,
    Lazy<IMailingService> mailService,
    Lazy<IOceanBookingService> oceanBookingService
    ) : base(dbContextFactory)
{
    _blobStorageService = blobStorageService;
    _emailService = emailService;
    _locationService = locationService;
    _mailService = mailService;
    _xmlHelper = new XMLHelper();
    _oceanBookingService = oceanBookingService;

    Aspose.Pdf.License license = new Aspose.Pdf.License();
    license.SetLicense("Aspose.Total.lic");
    license.Embedded = true;

    Aspose.Words.License wlicense = new Aspose.Words.License();
    wlicense.SetLicense("Aspose.Total.lic");
    Aspose.Cells.License clicense = new Aspose.Cells.License();
    clicense.SetLicense("Aspose.Total.lic");
}

Here's the full error message we're seeing. 这是我们看到的完整错误消息。


> An error occurred when trying to create a controller of type
> 'MyController'. Make sure that the controller has a parameterless
> public constructor. Resolution of the dependency failed, type =
> "ACME.AcmeApp.API.Controllers.MyController", name = "(none)".
> Exception occurred while: Calling constructor
> ACME.AcmeApp.Services.Services.DocumentService(ACME.AcmeApp.DAL.Interfaces.IDbContextFactory
> dbContextFactory, ACME.AcmeApp.Services.Interfaces.IBlobStorageService
> blobStorageService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IEmailService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] emailService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.ILocationService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] locationService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IMailingService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] mailService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IOceanBookingService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] oceanBookingService). Exception is:
> IndexOutOfRangeException - Index was outside the bounds of the array.
> ----------------------------------------------- At the time of the exception, the container was:
> 
>   Resolving ACME.AcmeApp.API.Controllers.MyController,(none)  
> Resolving parameter "documentService" of constructor
> ACME.AcmeApp.API.Controllers.MyController(ACME.AcmeApp.DAL.Interfaces.IDbContextFactory
> dbFactory, ACME.AcmeApp.Services.Interfaces.ISiService siService,
> ACME.AcmeApp.Services.Interfaces.IStorageService storageService,
> ACME.AcmeApp.Services.Services.IRepairService repairService,
> ACME.AcmeApp.Services.Interfaces.IDocumentService documentService,
> ACME.AcmeApp.Services.Interfaces.IImageService ImageService,
> ACME.AcmeApp.Services.Interfaces.INoteService noteService,
> ACME.AcmeApp.Services.Interfaces.IVisitService visitService,
> ACME.AcmeApp.Services.Interfaces.IOrderService orderService,
> ACME.AcmeApp.Services.Interfaces.IAuditService auditService,
> ACME.AcmeApp.Services.Interfaces.INotificationService
> notificationService, ACME.AcmeApp.Services.Interfaces.ILocationService
> locationService, ACME.AcmeApp.Services.ICustomerService
> customerSerivce, ACME.AcmeApp.Services.Interfaces.ICommonService
> commonService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.ILegService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] legService)
>     Resolving ACME.AcmeApp.Services.Services.DocumentService,(none) (mapped from ACME.AcmeApp.Services.Interfaces.IDocumentService,
> (none))
>     Calling constructor ACME.AcmeApp.Services.Services.DocumentService(ACME.AcmeApp.DAL.Interfaces.IDbContextFactory
> dbContextFactory, ACME.AcmeApp.Services.Interfaces.IBlobStorageService
> blobStorageService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IEmailService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] emailService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.ILocationService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] locationService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IMailingService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] mailService,
> System.Lazy`1[[ACME.AcmeApp.Services.Interfaces.IOceanBookingService,
> ACME.AcmeApp.Services, Version=1.0.0.0, Culture=neutral,
> PublicKeyToken=null]] oceanBookingService)  Index was outside the
> bounds of the array.

I realize this isn't an answer as such, but I hope you can use it anyway, it's too long for a comment, so here goes: 我意识到这并不是一个答案,但是我希望您仍然可以使用它,因为评论太长了,所以这里是:

I don't believe the error is related to Unity as such. 我不认为该错误与Unity本身有关。 Unity is telling you that the DocumentService constructor is throwing an exception. Unity告诉您DocumentService构造函数引发异常。 I would proceed with the following steps to investigate further: 我将按照以下步骤进行进一步调查:

  1. If possible, move all the Aspose license initialization code to a static method, and call it once at application startup time. 如果可能,请将所有Aspose许可证初始化代码移至静态方法,并在应用程序启动时调用一次。 There's no reason to waste time having Aspose look for license files over and over again 没有理由浪费时间让Aspose一遍又一遍地寻找许可证文件
  2. Inject a logger into the documentservice, wrap the constructor body in a try/catch, and log the exception to your logger 将记录器注入documentservice,将构造函数主体包装在try / catch中,然后将异常记录到记录器中
  3. If there's anything interesting in the default constructor of XMLHelper, investigate this code as well. 如果XMLHelper的默认构造函数中有任何有趣的内容,也请研究此代码。 Since its constructed explicitly in the code rather than by Unity, this could also be the source of the error. 由于它是在代码中而不是由Unity显式构造的,因此这也可能是错误的根源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 unity DI错误-确保控制器具有无参数的公共构造函数 - unity DI error - Make sure that the controller has a parameterless public constructor 统一依赖性错误-确保控制器具有无参数的公共构造函数 - unity dependency error -make sure that the controller has a parameterless public constructor WebApi DI Autofac - 确保控制器具有无参数的公共构造函数 - WebApi DI Autofac - Make sure that the controller has a parameterless public constructor 解析控制器:“确保控制器具有无参数的公共构造函数” - Resolve controller: “Make sure that the controller has a parameterless public constructor” Unity.WebApi | 确保控制器具有无参数的公共构造函数 - Unity.WebApi | Make sure that the controller has a parameterless public constructor MVC - IoC Unity - 确保控制器具有无参数的公共构造函数 - MVC - IoC Unity - Make sure that the controller has a parameterless public constructor 确保 controller 在 Unity 中有一个无参数的公共构造函数 - Make sure that the controller has a parameterless public constructor in Unity Ninject Web Api“确保控制器具有无参数的公共构造函数。” - Ninject Web Api “Make sure that the controller has a parameterless public constructor.” 确保 controller 具有无参数公共构造函数。 Mvc分层架构 - Make sure that the controller has a parameterless public constructor. Mvc layered architecture 确保控制器具有无参数的公共构造函数? - Make sure that the controller has a parameterless public constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM