autofac / Autofac.Extras.AggregateService

Dynamic aggregate service implementation generation for Autofac IoC
MIT License
4 stars 7 forks source link

Fixed KeyNotFoundException when resolving open generics via AggregateService methods #1

Closed kcuzner closed 9 years ago

kcuzner commented 9 years ago

I was using an AggregateService the other day with a method that was generic with the intention of using it to resolve open generics on the fly. Calling this method resulted in a KeyNotFoundException.

public interface IOpenGenericAggregate
{
    IOpenGeneric<T> GetOpenGeneric<T>();
}

public class OpenGeneric<T> : IOpenGeneric<T>
{
}

Test which fails:

var builder = new ContainerBuilder();
builder.RegisterAggregateService<IOpenGenericAggregate>();
builder.RegisterGeneric(typeof(OpenGeneric<>))
    .As(typeof(IOpenGeneric<>));

var container = builder.Build();

var aggregateService = container.Resolve<IOpenGenericAggregate>();

var generic = aggregateService.GetOpenGeneric<object>(); //KeyNotFoundException here
Assert.That(generic, Is.Not.Null);

I made a couple changes to fix the issue. If this isn't an intended use of the AggregateService I'm ok with these not being included.

The problem was that the existing interceptor stored the generic method definition in the invocation map, but the filled-in generic method (is there a term for that? Closed generic?) was used in the lookup. My changes do the following:

All of the tests pass, including the new one I added for testing the resolution of open generics. If there are any issues with the changes I'm more than willing to fix them.

Thanks.

tillig commented 9 years ago

Sorry for the delay on this. It looks pretty good so I'll pull it into the 4.0 branch. (You may not see this as accepted from a GitHub standpoint, but it's getting accepted sorta manually. :) )

tillig commented 9 years ago

Accepted into develop branch. Thanks!

tillig commented 9 years ago

Pushed to NuGet in release 4.0.0.