MEF registered types with dependencies on non MEF registered types doesn't work

213 views
Skip to first unread message

Shandem

unread,
Nov 25, 2010, 1:51:01 AM11/25/10
to Autofac
Hi,

Say i have an object in my main DLL:

TestData

then i register this type in my container:
builder.RegisterType<TestData>()

Say i have an object in my plugins DLL:

[Export]
public class MyObject {
public MyObject(TestData testData) {...}
}

then i register my plugins DLL:
var catalog = new DirectoryCatalog(m_HttpServer.MapPath("~/Plugins/
Stuff"));
builder.RegisterComposablePartCatalog(catalog);

when i resolve MyObject, it tells me that:

The request service 'ContractName=TestData' has not been registered.

I've looked in the source of Autofac and it appears as though
ComponentsForContract it checks if components are registered using the
RegistrationsFor method. Inside of that method, it ONLY looks for
services that are of type:
ContractBasedService

Unfortunately, the TestData service isn't registered as a
ContractBased service and only as a TypedService because it wasn't
registered using the MEF extensions.

A 'workaround' which does work is to register my test service 2 times
and mark it with an Export attribute:


builder.RegisterType<TestData>()
builder.RegisterComposablePartType(typeof(TestData));

This DOES work but is not really ideal.

Is there any reason why Autofac forces the search for dependencies on
MEF components to be ContractBasedService ?

Nicholas Blumhardt

unread,
Nov 25, 2010, 4:33:57 PM11/25/10
to aut...@googlegroups.com
Hi Shandem,

You need to mark the TestData type:

builder.RegisterType<TestData>()
    .Exported(x => x.As<TestData>());

There are differences between the way MEF and Autofac handle service contracts that make this necessary for a correct integration. Given that the exported types form the "public" extensibility API of your application, it is probably not a bad idea to select them explicitly anyway.

Cheers,
Nick


--
You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.


Shandem

unread,
Nov 25, 2010, 8:38:42 PM11/25/10
to Autofac
Ok great, but if i register TestData like the above, will it be able
to be injected into non-MEF dependencies as well?

Thanks,
Shannon

On Nov 26, 8:33 am, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Hi Shandem,
>
> You need to mark the TestData type:
>
> builder.RegisterType<TestData>()
>     .Exported(x => x.As<TestData>());
>
> There are differences between the way MEF and Autofac handle service
> contracts that make this necessary for a correct integration. Given that the
> exported types form the "public" extensibility API of your application, it
> is probably not a bad idea to select them explicitly anyway.
>
> Cheers,
> Nick
>
> > autofac+u...@googlegroups.com<autofac%2Bunsu...@googlegroups.com>
> > .

Nicholas Blumhardt

unread,
Nov 25, 2010, 10:03:41 PM11/25/10
to aut...@googlegroups.com
Yes.

Cheers,
Nick

To unsubscribe from this group, send email to autofac+u...@googlegroups.com.

Shandem

unread,
Nov 25, 2010, 11:41:20 PM11/25/10
to Autofac
Amazing. thanks for the tip, works perfectly!

On Nov 26, 2:03 pm, Nicholas Blumhardt <nicholas.blumha...@gmail.com>
wrote:
> Yes.
>
> Cheers,
> Nick
> > <autofac%2Bunsu...@googlegroups.com<autofac%252Buns...@googlegroups.com>
Reply all
Reply to author
Forward
0 new messages