Windsor TypedFactoryFacility and AutoMapper

556 views
Skip to first unread message

Jochen Jonckheere

unread,
Feb 17, 2012, 6:54:43 AM2/17/12
to castle-pro...@googlegroups.com
Hi,

Recently I tried to update to Windsor 3.0 in my project. Everything compiled, but when I started running my AutoMapper mappings that are also registered in the container couldn't be resolved anymore.
When I first tried to isolate the code in a new project, it worked. But then I added the TypedFactoryFacility and bang, exception.

This gist contains all the code needed to reproduce the problem: https://gist.github.com/1852705#file_windsor_automapper_test_fixture.cs

And here is the exception:

Castle.MicroKernel.ComponentActivator.ComponentActivatorException : ComponentActivator: could not instantiate WindsorAutoMapper.DummyMapper
  ----> System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
  ----> Castle.MicroKernel.ComponentNotFoundException : No component for supporting the service System.String was found

at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructorObject[] argumentsType implType)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext contextConstructorCandidate constructorObject[] arguments)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext contextBurden burden)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext contextBoolean trackedExternally)
at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve(CreationContext contextIReleasePolicy releasePolicy)
at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext contextBoolean requiresDecommissionBoolean instanceRequiredref Burden burden)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext contextBoolean instanceRequired)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handlerType serviceIDictionary additionalArgumentsIReleasePolicy policy)
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type serviceIDictionary argumentsIReleasePolicy policy)
at Castle.Windsor.WindsorContainer.Resolve()
at WindsorAutoMapper.WindsorAutomapperTestFixture.DummyMapperCanNotBeResolvedIfTheTypedFactoryFacilityIsRegistered() in WindsorAutomapperTestFixture.cs: line 64
--TargetInvocationException
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo methodObject targetObject[] argumentsref SignatureStruct sigMethodAttributes methodAttributesRuntimeType typeOwner)
at System.Reflection.RuntimeConstructorInfo.Invoke(Object objBindingFlags invokeAttrBinder binderObject[] parametersCultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object objObject[] parameters)
at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstanceCore(ConstructorCandidate constructorObject[] argumentsType implType)
--ComponentNotFoundException
at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type serviceIDictionary argumentsIReleasePolicy policy)
at Castle.Facilities.TypedFactory.TypedFactoryComponentResolver.Resolve(IKernelInternal kernelIReleasePolicy scope)
at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Resolve(IInvocation invocation)
at Castle.Facilities.TypedFactory.Internal.TypedFactoryInterceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.Func`2Proxy.Invoke(String arg)
at AutoMapper.TypeMapFactory.<>c__DisplayClass11.<FindTypeMember>b__d(MemberInfo prop)
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 sourceFunc`2 predicate)
at AutoMapper.TypeMapFactory.FindTypeMember(IEnumerable`1 modelPropertiesIEnumerable`1 getMethodsString nameToSearchIMappingOptions mappingOptions)
at AutoMapper.TypeMapFactory.MapDestinationPropertyToSource(LinkedList`1 resolversTypeInfo sourceTypeString nameToSearchIMappingOptions mappingOptions)
at AutoMapper.TypeMapFactory.CreateTypeMap(Type sourceTypeType destinationTypeIMappingOptions options)
at AutoMapper.ConfigurationStore.CreateTypeMap(Type sourceType destinationString profileName)
at AutoMapper.ConfigurationStore.CreateMap(String profileName)
at WindsorAutoMapper.Mapper`2..ctor(IConfiguration configurationIMappingEngine mappingEngine) in Mapper.cs: line 23
at WindsorAutoMapper.DummyMapper..ctor(IConfiguration configurationIMappingEngine mappingEngine) in DummyMapper.cs: line 7

Is this a bug in Castle Windsor? Or am I doing something wrong in my AutoMapper registration?

Regards,

Jochen Jonckheere

Krzysztof Koźmic

unread,
Feb 17, 2012, 7:59:36 PM2/17/12
to castle-pro...@googlegroups.com
Hi Jochen,

The reason why you're seeing this issue is as follows:

* You're registering Automapper's ConfigurationStore class in the container
* The class has (among others) the following two publicly settable properties:
        public Func<string, string> SourceMemberNameTransformer { get; set; }
        public Func<string, string> DestinationMemberNameTransformer { get; set; }
* With default settings Windsor treats publicly settable properties as optional dependencies that it will try to satisfy
* TypedFactoryFacility is the one providing values for those dependencies, injecting delegate-based typed factories into those properties
* Automapper thinks you're overriding its defaults and will call those injected Func typed factories, which will trigger Windsor to resolve 'string' service, which is why it fails.

So the problem is twofold:

1. You're exposing those properties in the container. You can chose not to do it by specifying the following when registering the component:
                    .Properties(PropertyFilter.IgnoreAll)
This will make your tests pass.

2. Typed factory facility should ignore Func<string> since Windsor does not allow services of type string anyway. This is a bug in the facility, please report it to the issue tracker.

@K
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/castle-project-users/-/GqCYuXt4pWsJ.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.

Jochen Jonckheere

unread,
Feb 20, 2012, 3:56:32 AM2/20/12
to castle-pro...@googlegroups.com
Hello Krzysztof,

The test passes and I can start using Windsor 3 in my projects now.

The bug for the TypeFactoryFacility has been added in the issue tracker as you requested.

Thanks,

Jochen

Krzysztof Koźmic

unread,
Feb 21, 2012, 12:47:37 AM2/21/12
to castle-pro...@googlegroups.com
cheers,
@K
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
Reply all
Reply to author
Forward
0 new messages