0
votes

In an ASP.NET app, when trying to work with both Simple Injector and ELMAH, the following get request returns a 500 error:

GET /elmah.axd/stylesheet returns a 500 error.

The error message:

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.

Stack trace:

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)

Following the "for more information link" mentioned above, there is a further link to some documentation on how to try to fix these types of errors. The link is: https://simpleinjector.readthedocs.io/en/latest/extensibility.html#overriding-constructor-resolution-behavior

There, 2 fixes are proposed. I tried both fixes, and for each, got the following different errors.

First Fix

Where the GreediestConstructorBehavior is used. This fix yielded the following error message and stack trace:

First Fix Error Message:

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.

First Fix Stack Trace:

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)

Second Fix

Where the MostResolvableParametersConstructorResolutionBehavior is used. This second fix yielded the following error message and stack trace:

Second Fix Error Message:

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.

Second Fix Stack Trace

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)

Thanks to anyone who can help out with this.

1

1 Answers

1
votes

Fixed by updating the PageInitializerModule class so that Simple Injector ignores the ELMAH handlers:

Before change:

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

Changed to:

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