Please note that the issue I'm experiencing is NOT a generic "IndexOutOfRangeException", which I am well familiar with. 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.
I am using a Unity DI container within the context of an ASP.NET Web API back end written in C#. 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".
We've tried using IIS configuration to recycle the app pool on a regular basis, but we are still getting this error.
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.
ACME.AcmeApp.Services.Services.DocumentService
? – Jonas Høgh