Search code examples
iisasp.net-web-api2mef

MEF: Assemblies getting removed from catalog?


I've added MEF to my web api project using IDependencyExplorer and all works fine.

However, after a few minutes of running, I start to get export not found errors. When I check the catalog in the debugger, the exports have gone and the assembly that contains the exported type is no longer there.

We aren't removing assemblies from the catalog explicity, so I'm baffled as to why this is happening. Would anyone be able to shed some light on this?


Solution

  • Just figured out what was happening.

    I use an aggregate catalog and add the assemblies containing my exports into it.

    The first time the web site starts, all required assemblies were loaded in the current app domain, so everything was working fine.

    Later on the IIS process was recycling/restarting. When this happens and a new web request is received, not all the assemblies I needed were loaded, and I had put a guard to only add loaded assemblies into the aggregate catalog.

    The solution is to force the loading of the assemblies if they have not been loaded yet, or to use a directory catalog instead.

    Hope this helps anyone else who experiences this issue.