mark
mark

Reputation: 62774

5 years later - is there lazy dll load in MEF?

Right now we initialize MEF using in essence the directory catalogue. I.e. all the assemblies are loaded, even those which are not used.

Well, we have modified it a bit - all the assemblies are inspected in the reflection only scope to see if they have a certain assembly attribute and only those assemblies that do are picked.

Still, there is nothing lazy about it. A feature may never actually be used, but the assembly implementing it is loaded anyway.

I have found this 5 year old question on the same subject - Lazy Loading DLL's with MEF. If I understood the answer correctly there was no lazy loading at the time (the link to community does not give me anything useful).

Is it still the situation? Can we have lazy dll loading?

This is how I would like it to work:

I would like a situation where as long as any code depending on ISomeFeature is not running, the assembly implementing SomeFeatureImpl is not loaded. Obviously, when executing code depending on ISomeFeature, the actual implementation must be loaded (if not already) and the dependency successfully resolved.

Am I asking for too much? Does MEF support it?

Upvotes: 3

Views: 417

Answers (1)

Adi Lester
Adi Lester

Reputation: 25201

Well, maybe not five years later, but is six OK?

I've recently implemented just the thing you are talking about - lazy assembly loading in MEF. Like you said, it will only load the plugins which are actually being used by the application, while still allowing you to inspect their metadata. To do so, however, you will need to serialize the part information for each assembly beforehand (probably during the plugin's post-build event). Do note that for this solution, for the assemblies to be loaded lazily, imports have to be wrapped in Lazy<T>.

You're welcome to have a look at the project and also contribute to it.

Upvotes: 2

Related Questions