jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
327 stars 89 forks source link

ReflectionTypeLoadException on AddMediatR in Azure Web App #70

Closed geirsagberg closed 5 years ago

geirsagberg commented 5 years ago

Using MediatR 6.0.0 and MediatR.Extensions..Microsoft.DependencyInjection 6.0.1.

Get the following exception on Startup when hosted in an Azure Web App when the Application Insights Site Extension is enabled:

Application startup exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.ApplicationInsights, Version=2.9.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at MediatR.ServiceCollectionExtensions.<>c.<ConnectImplementationsToTypesClosing>b__9_0(Assembly a)
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at MediatR.ServiceCollectionExtensions.ConnectImplementationsToTypesClosing(Type openRequestInterface, IServiceCollection services, IEnumerable`1 assembliesToScan, Boolean addIfAlreadyExists)
   at MediatR.ServiceCollectionExtensions.AddMediatRClasses(IServiceCollection services, IEnumerable`1 assembliesToScan)
   at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services, IEnumerable`1 assemblies, Action`1 configuration)
   at MediatR.ServiceCollectionExtensions.AddMediatR(IServiceCollection services)
   at Gaver.Web.Startup.ConfigureServices(IServiceCollection services) in d:\a\1\s\src\Gaver.Web\Startup.cs:line 64
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ApplicationInsights, Version=2.9.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.ApplicationInsights, Version=2.9.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

Seems very related to this stackoverflow issue.

Maybe ConnectImplementationsToTypesClosing could use some kind of try-catch similar to the solutions for that issue, for the cases when an assembly isn't found for some reason?