ELMAH样式表无法使用Simple Injector

时间:2018-04-12 09:35:59

标签: c# asp.net elmah simple-injector

在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)

感谢任何可以提供帮助的人。

1 个答案:

答案 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() { }
}