Generic interface and class

Topics: Writing modules
Jun 19, 2012 at 9:36 AM

We have inteface definition:

public interface IManyQueue<T> : IDependency
    {
        IEnumerable<T> Dequeue();
        IEnumerable<T> Dequeue(int count);
        void Enqueue(IEnumerable<T> values);
        void Clear();
    }

And class:

public class ManyQueue<T> : IManyQueue<T>
    {
        private readonly IRepository<QueueRecord> repository;

        public ManyQueue(IRepository<QueueRecord> repository)
        {
            this.repository = repository;
        }

        public void Enqueue(IEnumerable<T> values)
        {
//
        }

        public IEnumerable<T> Dequeue()
        {
//
        }

        public IEnumerable<T> Dequeue(int count)
        {
//
        }

        public void Clear()
        {
//
        }
    }

And module "override":

public class CaliperModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {         
           builder.RegisterGeneric(typeof(ManyQueue<>)).As(typeof(IManyQueue<>));
        }
    }

After our module enabled orchard reports this errors:
Orchard.Environment.DefaultOrchardHost: ERROR - A tenant could not be started: Default
System.ArgumentException: The type 'Caliper.Orchard.Tools.ManyQueue`1[T]' is not assignable to service 'Caliper.Orchard.Tools.IManyQueue`1'.
   at Autofac.Builder.RegistrationBuilder.CreateRegistration(Guid id, RegistrationData data, IInstanceActivator activator, IEnumerable`1 services, IComponentRegistration target)
   at Autofac.Builder.RegistrationBuilder.CreateRegistration[TLimit,TActivatorData,TSingleRegistrationStyle](IRegistrationBuilder`3 rb)
   at Autofac.Builder.RegistrationBuilder.RegisterSingleComponent[TLimit,TActivatorData,TSingleRegistrationStyle](IComponentRegistry cr, IRegistrationBuilder`3 rb)
   at Autofac.RegistrationExtensions.<>c__DisplayClassd.<RegisterType>b__c(IComponentRegistry cr)
   at Autofac.ContainerBuilder.Build(IComponentRegistry componentRegistry, Boolean excludeDefaultModules)
   at Autofac.ContainerBuilder.Update(IComponentRegistry componentRegistry)
   at Autofac.Core.Lifetime.LifetimeScope.CreateScopeRestrictedRegistry(Object tag, Action`1 configurationAction)
   at Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope(Object tag, Action`1 configurationAction)
   at Orchard.Environment.ShellBuilders.ShellContainerFactory.CreateContainer(ShellSettings settings, ShellBlueprint blueprint) in D:\Projects\codeplex-orchard\src\Orchard\Environment\ShellBuilders\ShellContainerFactory.cs:line 47
   at Orchard.Environment.ShellBuilders.ShellContextFactory.CreateShellContext(ShellSettings settings) in D:\Projects\codeplex-orchard\src\Orchard\Environment\ShellBuilders\ShellContextFactory.cs:line 61
   at Orchard.Environment.DefaultOrchardHost.CreateShellContext(ShellSettings settings) in D:\Projects\codeplex-orchard\src\Orchard\Environment\DefaultOrchardHost.cs:line 174
   at Orchard.Environment.DefaultOrchardHost.CreateAndActivateShells() in D:\Projects\codeplex-orchard\src\Orchard\Environment\DefaultOrchardHost.cs:line 134
A first chance exception of type 'System.ArgumentException' occurred in Autofac.DLL
In Orchard 1.2 our module works great :-(.
Can you help us to run our code :-)?

Developer
Jun 19, 2012 at 1:02 PM
Edited Jun 19, 2012 at 1:02 PM

Orchard's dependency auto-registration doesn't handle generics, so you correctly handled it with an Autofac module. However with this approach you shouldn't implement IDependency.

Once generic dependencies will get support, you can re-add the marker interface, but till then, it's unneeded and also harmful, as you've seen.

BTW the I think you experienced the issue after the upgrade because with Orchard 1.4 also Autofac was updated.

Jun 19, 2012 at 3:10 PM

I remove IDependecy marker from interfaces. I have the same problem :-( 

I will try resolve this later.