当应用程序在网络上共享时,MEF的组成

问题描述:

我有一个MEF应用程序,在本地运行时效果很好,但在远程调用网络共享时无效。当应用程序在网络上共享时,MEF的组成

我正在使用Assembly.LoadFrom来避免UNC问题,但是看到所有的DLL都位于exe旁边,我并不认为这会是问题,但我试过任何方法。

在查看msdn后,我还修复了ConfigurationManager.GetSection问题,这似乎是.NET 4权限的常见问题。

我允许<loadFromRemoteSources enabled="true"/>在配置文件中。所以我不确定问题出在哪里。

编辑: ProductDispatcher中的异常在catalog.Parts中是明确的。

的代码建立容器和目录:

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); 

导入是(我曾尝试使公共):

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

一个出口的一个例子是:

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

我得到的异常错误是:

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 

似乎是MEF在部分信任场景中不能很好地工作。有什么我需要做的,以确保一切都在充分信任下运行?

+0

我有我的项目同样的问题,但我很遗憾始终没能解决这个问题。 :( – eandersson 2012-03-29 14:01:28

尽管您已启用“从远程源加载”,但可能会发生这种情况,因为这些文件可能对它们仍有限制。

NTFS支持将元数据应用于备用数据流(ADS)中的文件的功能。这将包括区域信息(例如互联网区域等)。

这可能是导致网络位置文件出现问题的原因,它们可能会归入Internet区域,因此仍有可能被阻止。

看看这篇文章,看看这是否会解决这个问题为您:http://mikehadlow.blogspot.co.uk/2011/07/detecting-and-changing-files-internet.html