Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Generic types won't register in the catalog

Tags:

c#

mef

We're using .NET 4.5 and have had no problem with open generic exports in MEF up until a month ago when it suddenly stopped working. 70 tests on our CI server turned red and we traced it down to missing parts in the container.

I found it strange, so I wrote this test code:

var catalog = new TypeCatalog(typeof(Impersonator<>), typeof(Cache<>));
var parts = catalog.Parts;

But it looks like none of the open generic types will register in the catalog. Parts is a TypeCatalog with Types = <Empty>.

Usually we go about this by declarative exports using attributes as in this SO question, but none of the alternatives seems to be working anymore.

Any idea will be most appreciated.

like image 336
Mikael Lundin Avatar asked Aug 09 '13 13:08

Mikael Lundin


3 Answers

Any idea what triggered it to start failing? Did you get a .NET update in that timeframe?

Out of curiosity if you write a simple console app targeting .NET 4.5 does this issue repro?

The reason I ask is I believe there was an update released for .NET 4.5 to make sure open generics didn't break existing .NET 4.0 apps and so if for some reason your tests are running in .NET 4.0 mode then then the open generic support will be disabled which could cause what you are seeing.

like image 84
Wes Haggard Avatar answered Nov 07 '22 14:11

Wes Haggard


I resolved this in my web app by putting the entry < httpRuntime targetFramework="4.5" /> in my web.config. For non-web apps, make sure the app.config has the entry < supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />.

see http://blogs.msdn.com/b/webdev/archive/2012/11/19/all-about-httpruntime-targetframework.aspx for more info.

like image 21
Hasani Blackwell Avatar answered Nov 07 '22 15:11

Hasani Blackwell


I know this is an old issue, but I have some additional information that may help others.

I ran into the same issue. My unit tests were working then suddenly stopped. We narrowed it down to a pair of Windows updates: KB2840642v2 and KB2840642. After uninstalling these, MEF started working properly again in unit tests. I didn't see anything in the release notes for those updates that should affect things, but you know how that goes.

However, I am now running Windows 8 unfortunately, and the issue has popped up again. I do not have those updates installed. I think they were incorporated natively into the operating system.

like image 24
terryagi Avatar answered Nov 07 '22 14:11

terryagi