AdminMenu.cs -> Constructor with custom parameters

Topics: Writing modules
Jan 8, 2012 at 3:35 PM
Edited Jan 8, 2012 at 3:36 PM

Hi there.

I'm writing a module and need to pass a custom parameter to AdminMenu class constructor in AdminMenu.cs like this:

        public AdminMenu(IShowcaseService showcaseServices)
        {
            _showcaseServices = showcaseServices;
        }

Similar to what Szmyd made in his AdvancedMenu module:

        public AdminMenu(IAdvancedMenuService menuServices) {
            _menuServices = menuServices;
        }

Now I'm getting an error saying:

 

None of the constructors found with policy 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'TTS.AwkwardShowcase.AdminMenu' can be invoked with the available services and parameters:
Constructor 'Void .ctor(TTS.AwkwardShowcase.Services.IShowcaseService)' parameter resolution failed at parameter 'TTS.AwkwardShowcase.Services.IShowcaseService showcaseServices'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Autofac.Core.DependencyResolutionException: None of the constructors found with policy 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'TTS.AwkwardShowcase.AdminMenu' can be invoked with the available services and parameters:
Constructor 'Void .ctor(TTS.AwkwardShowcase.Services.IShowcaseService)' parameter resolution failed at parameter 'TTS.AwkwardShowcase.Services.IShowcaseService showcaseServices'.

Can, anyone please point me where to dig to make this work? (Szmyd's constructor works, obviously, but I suspect it's made somehow not in a module code)

 

Jan 8, 2012 at 3:43 PM

IShowcaseService itself needs to inherit one of the autowiring interfaces, usually IDependency (but in some situations you can ISingletonDependency or ITransientDependency, make sure you know exactly what they do before trying to use them tho!)

Jan 8, 2012 at 3:46 PM

Yep, I have:

    public interface IShowcaseService : IDependency

Jan 8, 2012 at 7:01 PM

Nobody knows?

Jan 9, 2012 at 1:13 AM

Can I check you've actually implemented an instance of IShowcaseService?

Jan 9, 2012 at 9:10 AM
Edited Jan 9, 2012 at 9:17 AM

Ofc I did.

Well, after some digging I have a strong feelling it's about autofac not registering generic types (this issue).

Jan 9, 2012 at 1:52 PM

No it's much simpler, the implementation you posted (but have now deleted) was omitting "public" at the start of the class definition. Autofac can't wire up a private implementation for obvious reasons...

Jan 9, 2012 at 7:42 PM
Edited Jan 9, 2012 at 7:43 PM

LOL me! Thanks for noticing that "public", although by the moment you answeared I had implemented this to make things work:

 

    public class AwkwardShowcaseModule : Module
    {
        protected override void Load(ContainerBuilder moduleBuilder)
        {
            moduleBuilder.RegisterType<ShowcaseService>().As<IShowcaseService>();
        }

    }

Thanks again, your way Is much better ;)

Jan 9, 2012 at 8:49 PM

Forcing the dependency to register like that might work but would probably error in a less than full trust environment.

But yeah it can be easy to miss something small like "public", especially when you've been staring at a piece of code for a while, luckily I've developed a keen eye for the little details over many years of software development ;)