[英]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:
我将按照以下步骤进行进一步调查:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.