在ASP.NET应用程序中,当尝试使用Simple Injector和ELMAH时,以下get请求返回500错误:
GET /elmah.axd/stylesheet返回500错误。
无法找到ManifestResourceHandler类型的注册,也无法进行隐式注册。要使容器能够创建ManifestResourceHandler,它应该只有一个公共构造函数:它有2个。有关更多信息,请参阅https://simpleinjector.org/one-constructor。
SimpleInjector.ActivationException: No registration for type ManifestResourceHandler could be found and an implicit registration could not be made. For the container to be able to create ManifestResourceHandler it should have only one public constructor: it has 2. See https://simpleinjector.org/one-constructor for more information.
at SimpleInjector.Container.ThrowNotConstructableException(Type concreteType)
at SimpleInjector.Container.ThrowMissingInstanceProducerException(Type serviceType)
at SimpleInjector.Container.ThrowInvalidRegistrationException(Type serviceType, InstanceProducer producer)
at SimpleInjector.Container.GetRegistration(Type serviceType, Boolean throwOnFailure)
at WebApp.Global.InitializeHandler(IHttpHandler handler) in .......\Global.asax.cs:line 63
at WebApp.PageInitializerModule.<>c__DisplayClass1_0.<System.Web.IHttpModule.Init>b__0(Object sender, EventArgs e) in .......\Global.asax.cs:line 48
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
在上面提到的“更多信息链接”之后,还有一些关于如何尝试修复这些类型错误的文档的链接。链接是: https://simpleinjector.readthedocs.io/en/latest/extensibility.html#overriding-constructor-resolution-behavior
在那里,提出了2个修复。我尝试了两个修复程序,并且每个修复程序都有以下不同的错误。
使用GreediestConstructorBehavior
的地方。此修复程序产生以下错误消息和堆栈跟踪:
无法找到ManifestResourceHandler类型的注册,也无法进行隐式注册。 ManifestResourceHandler类型的构造函数包含String类型的参数'resourceName',它不能用于构造函数注入。
SimpleInjector.ActivationException: No registration for type ManifestResourceHandler could be found and an implicit registration could not be made. The constructor of type ManifestResourceHandler contains parameter 'resourceName' of type String which can not be used for constructor injection.
at SimpleInjector.Container.ThrowNotConstructableException(Type concreteType)
at SimpleInjector.Container.ThrowMissingInstanceProducerException(Type serviceType)
at SimpleInjector.Container.ThrowInvalidRegistrationException(Type serviceType, InstanceProducer producer)
at SimpleInjector.Container.GetRegistration(Type serviceType, Boolean throwOnFailure)
at WebApp.Global.InitializeHandler(IHttpHandler handler) in .......\Global.asax.cs:line 63
at WebApp.PageInitializerModule.<>c__DisplayClass1_0.<System.Web.IHttpModule.Init>b__0(Object sender, EventArgs e) in .......\Global.asax.cs:line 48
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
使用MostResolvableParametersConstructorResolutionBehavior
的地方。第二个修复产生了以下错误消息和堆栈跟踪:
无法找到ManifestResourceHandler类型的注册,也无法进行隐式注册。为了使容器能够创建ManifestResourceHandler,它应该包含一个只包含可以解析的参数的公共构造函数。
SimpleInjector.ActivationException: No registration for type ManifestResourceHandler could be found and an implicit registration could not be made. For the container to be able to create ManifestResourceHandler, it should contain a public constructor that only contains parameters that can be resolved.
at SimpleInjector.Container.ThrowNotConstructableException(Type concreteType)
at SimpleInjector.Container.ThrowMissingInstanceProducerException(Type serviceType)
at SimpleInjector.Container.ThrowInvalidRegistrationException(Type serviceType, InstanceProducer producer)
at SimpleInjector.Container.GetRegistration(Type serviceType, Boolean throwOnFailure)
at WebApp.Global.InitializeHandler(IHttpHandler handler) in .......\Global.asax.cs:line 63
at WebApp.PageInitializerModule.<>c__DisplayClass1_0.<System.Web.IHttpModule.Init>b__0(Object sender, EventArgs e) in .......\Global.asax.cs:line 48
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
感谢任何可以提供帮助的人。
答案 0 :(得分:1)
通过更新PageInitializerModule
类来修复,以便Simple Injector忽略ELMAH处理程序:
public sealed class PageInitializerModule : IHttpModule
{
public static void Initialize()
{
DynamicModuleUtility.RegisterModule(typeof(PageInitializerModule));
}
void IHttpModule.Init(HttpApplication app)
{
app.PreRequestHandlerExecute += (sender, e) => {
var handler = app.Context.CurrentHandler;
if (handler != null)
{
string name = handler.GetType().Assembly.FullName;
if (!name.StartsWith("System.Web") &&
!name.StartsWith("Microsoft"))
{
Global.InitializeHandler(handler);
}
}
};
}
void IHttpModule.Dispose() { }
}
public sealed class PageInitializerModule : IHttpModule
{
public static void Initialize()
{
DynamicModuleUtility.RegisterModule(typeof(PageInitializerModule));
}
void IHttpModule.Init(HttpApplication app)
{
app.PreRequestHandlerExecute += (sender, e) => {
var handler = app.Context.CurrentHandler;
if (handler != null)
{
string name = handler.GetType().Assembly.FullName;
if (!name.StartsWith("System.Web") &&
!name.StartsWith("Microsoft") &&
!name.StartsWith("Elmah")) // <----- ADDED THIS -----
{
Global.InitializeHandler(handler);
}
}
};
}
void IHttpModule.Dispose() { }
}