我有一个 MEF 应用程序,它在本地运行时运行良好,但在网络共享上远程调用时不起作用。
我正在使用 Assembly.LoadFrom 来避免 UNC 问题,但看到所有 dll 都位于 exe 旁边,我并不认为这会是问题所在,但我尝试了任何方法。
在查看 msdn 后,我还修复了 ConfigurationManager.GetSection 问题,这似乎是 .NET 4 权限的常见问题。 .
我允许 <loadFromRemoteSources enabled="true"/>
在配置文件中。所以我不确定问题可能出在哪里。
编辑:异常(exception)中的 ProductDispatcher 在目录中是确定的。零件。
设置容器和目录的代码:
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 在部分信任的情况下工作得不太好。我需要做些什么来确保一切都在完全信任的情况下运行?
请您参考如下方法:
尽管您已启用“从远程源加载”,但这可能会发生,因为文件可能仍然对其有限制。
NTFS 支持将元数据应用到备用数据流 (ADS) 中的文件的能力。这将包括区域信息(例如 Internet 区域等)。
这可能是导致您的网络文件出现问题的原因,它们可能被归类在 Internet 区域中,因此仍有可能被阻止。
查看这篇文章,看看这是否会为您解决:http://mikehadlow.blogspot.co.uk/2011/07/detecting-and-changing-files-internet.html