big_tommy_7bb
big_tommy_7bb

Reputation: 1317

MEF Composition When Application Is On A Network Share

I've an MEF application that works great when run locally, but doesn't work when called remotely on a network share.

I'm using Assembly.LoadFrom to avoid UNC issues, but seeing as all the dlls are situated next to the exe I didn't really think this would be the problem, but I tried it any way.

I also fixed the ConfigurationManager.GetSection issues, that seems to be a common problem with .NET 4 permissions, after looking on msdn.

I'm allowing <loadFromRemoteSources enabled="true"/> in the config file. So I'm not sure where the problem could be.

Edit: The ProductDispatcher in the exception is definitey in the catalog.Parts.

The code setting up the container and catalog:

var catalog = new AggregateCatalog();

var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

foreach (string file in Directory.GetFiles(dir, "XXX*.dll"))
{
    var assembly = Assembly.LoadFrom(file);
    catalog.Catalogs.Add(new AssemblyCatalog(assembly));
}

var container = new CompositionContainer(catalog);
var batch = new CompositionBatch();
batch.AddPart(this);
container.Compose(batch);

The Import is (which I have tried making public):

[ImportMany(typeof(IEntityTypeDispatcher))]
private IEnumerable<IEntityTypeDispatcher> Dispatchers { get; set; }

An example of an export is:

[Export(typeof(IEntityTypeDispatcher))]
internal class ContactDispatcher : EntityTypeDispatcher<Contact>

The exception error I get is:

The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

Resulting in: An exception occurred while trying to create an instance of type 'XXX.XXX.Dispatch.ProductDispatcher'.

Resulting in: Cannot activate part 'XXX.XXX.Dispatch.ProductDispatcher'.
Element: XXX.XXX.Dispatch.ProductDispatcher -->  XXX.XXX.Dispatch.ProductDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot get export 'XXX.XXX.Dispatch.ProductDispatcher (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' from part 'XXX.XXX.Dispatch.ProductDispatcher'.
Element: XXX.XXX.Dispatch.ProductDispatcher (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.ProductDispatcher -->  AssemblyCatalog (Assembly="XXX.XXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")

Resulting in: Cannot set import 'XXX.XXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher")' on part 'XXX.XXX.Dispatch.DispatcherRepository'.
Element: XXX.XXX.Dispatch.DispatcherRepository.Dispatchers (ContractName="XXX.XXX.Dispatch.IEntityTypeDispatcher") -->  XXX.XXX.Dispatch.DispatcherRepository
 (System.ComponentModel.Composition.CompositionException)

   at System.ComponentModel.Composition.CompositionResult.ThrowOnErrors(AtomicComposition atomicComposition)
   at System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch batch)
   at System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch batch)
   at XXX.XXX.Dispatch.DispatcherRepository.LoadDispatchers() in D:\Workspaces\XXX\Branches\Dev1\XXX.XXX\XXX.XXX\Dispatch\DispatcherRepository.cs:line 71
   at XXX.XXX.Dispatch.DispatcherRepository.get_Instance() in D:\Workspaces\XXX\Branches\Dev1\XXX.XXX\XXX.XXX\Dispatch\DispatcherRepository.cs:line 34
   at XXX.XXX.Dispatch.DispatcherRepository.GetDispatchers() in D:\Workspaces\XXX\Branches\Dev1\XXX.XXX\XXX.XXX\Dispatch\DispatcherRepository.cs:line 21
   at XXX.XXX.Dispatch.Dispatcher.get_Instance() in D:\Workspaces\XXX\Branches\Dev1\XXX.XXX\XXX.XXX\Dispatch\Dispatcher.cs:line 30
   at XXX.XXX.Broker..ctor() in D:\Workspaces\XXX\Branches\Dev1\XXX.XXX\XXX.XXX\Broker.cs:line 52

It seems to be that MEF doesn't work too well in partial trust scenarios. Is there anything I need to do to ensure everything is running under full trust?

Upvotes: 5

Views: 781

Answers (1)

Matthew Abbott
Matthew Abbott

Reputation: 61599

Although you have enabled "load from remote sources", this might be occurring because the files may still have restrictions on them.

NTFS supports the ability to apply metadata to a file in an alternate data stream (ADS). This will include the Zone information (e.g. Internet Zone, etc.).

This might be what is causing issues with your network-located files, they may be classified in the Internet Zone, and therefore are still potentially being blocked.

Check out this article and see if this will resolve it for you: http://mikehadlow.blogspot.co.uk/2011/07/detecting-and-changing-files-internet.html

Upvotes: 3

Related Questions