[Windsor] Multiple registrations with Castle Windsor

91 views
Skip to first unread message

bdaniel7

unread,
May 29, 2015, 2:17:32 AM5/29/15
to castle-pro...@googlegroups.com

I'm reposting from here http://stackoverflow.com/questions/30510942/multiple-registrations-with-castle-windsor

I have the following situation in a WPF application:

public class ExpenseView : UserControl, IAccountingView {}

// just a marker, the contract is in IViewWindow
public interface IAccountingView : IViewWindow {}

And I need to be able to register ExpenseView to be resolvable in two ways, as concrete type, ExpenseView and as IAccountingView (and maybe as another interface as well).

I'm registering the types like this:

// I need a collection of IViewWindow to be used
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .BasedOn<IViewWindow>()
    .WithServiceFromInterface()
    .LifestyleTransient()
    );

// all other types don't have interfaces, are used as concrete types.
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .Where( type => type.IsPublic )
    .WithServiceSelf()
    .LifestyleTransient()
    );

Now, this works, ExpenseView and all other types are instantiated, except that when I need to use

var newInstance = container.Resolve( iView.ViewType );

to get another instance, where iView.ViewType is the concrete type ExpenseView (as per the example), I get this exception:

'Castle.MicroKernel.ComponentNotFoundException'  
No component for supporting the service ExpenseView was found.

Any ideas why is this happening and how to make it work?

Thank you,

Daniel

hammett

unread,
May 29, 2015, 3:49:28 AM5/29/15
to castle-pro...@googlegroups.com
WithServiceSelf should have done the trick.

The container has a nice debugger view - could you check what it shows
to you? just hit a breakpoint where the container instance is
accessible/in-the-scope, and see what is in there...
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to castle-project-u...@googlegroups.com.
> To post to this group, send email to castle-pro...@googlegroups.com.
> Visit this group at http://groups.google.com/group/castle-project-users.
> For more options, visit https://groups.google.com/d/optout.



--
Cheers,
hammett
http://www.d-collab.com/
http://www.hammettblog.com/

bdaniel7

unread,
May 29, 2015, 3:54:26 AM5/29/15
to castle-pro...@googlegroups.com
Hi,

Indeed, I should have used 

.BasedOn<IViewWindow>()
.WithServiceFromInterface()
.WithServiceSelf()

But I didn't know that Services are cumulative.
Or pay more attention...
Reply all
Reply to author
Forward
0 new messages