NET 4.5和Windows Store PCL的MEF 2合成 [英] MEF 2 Composition with .NET 4.5, Windows Store PCL
问题描述
我无法让MEF满足进口要求,可能需要一些帮助.
I am having trouble getting MEF to satisfy an import and could use some help.
不幸的是,该问题仅出现在生产代码库中.我试图简化问题,所以我可以举一个例子,看看我缺乏理解的地方,但是简化的版本行得通.因此,这要么是我遗漏了两者之间的差异(我已尽力仔细研究了两者),要么是复制的复杂性.
Unfortunately, the problem only manifests in a production code base. I tried to simplify the problem so I could post an example and see where my lack of understanding is, but the simplified version worked. So this is either I am missing the difference (and I have done my best to look thoroughly at the two), or the complexity is needed to reproduce.
我有一个WPF应用程序,它使用.NET 4.5类库和可移植类库(针对.net 4.5和Windows 8存储应用程序).我还没有Windows 8商店应用程序,但是已经计划好了(因此很头疼).我正在使用我最近从NuGet中提取的MEF 2:
I have a WPF application consuming .NET 4.5 class libraries and portable class libraries (targeting .net 4.5 and windows 8 store apps). I do not yet have a windows 8 store application, but it is planned (thus the headache). I am using MEF 2 that I pulled off of NuGet recently:
<package id="Microsoft.Composition" version="1.0.20" targetFramework="portable-net45+win" />
我想我正在寻找关于如何调试它的一些建议,因为我将无法发布实际的代码.我可以找到的有关调试的大多数在线建议似乎不适用于MEF 2,至少不是此PCL兼容版本.以下是我的简化版本,但再次可以使用此版本.
I guess what I am looking for is some advice on how to debug this, since I will not be able to post the actual code. Most of the online advice I can find on how to debug doesn't seem to work with MEF 2, at least not this PCL-compatible version. The following is my simplified version but, again, this version works.
namespace Portable.Contracts
{
public interface IExportable
{
string Name { get; }
}
}
namespace Desktop
{
[Export(typeof(IExportable))]
public class Exported : IExportable
{
public string Name
{
get { return "Exported"; }
}
}
}
namespace Portable
{
public class Importer
{
[Import]
public IExportable Exportable { get; set; }
public Importer()
{
MEFLoader.ResolveImports(this);
}
public string Name { get { return Exportable.Name; } }
}
}
namespace Portable
{
public class MEFLoader
{
private static CompositionHost Container { get; set; }
public static void SetContainer(CompositionHost container)
{
Container = container;
}
public static void ResolveImports(object target)
{
if(Container != null)
{
Container.SatisfyImports(target);
}
}
}
}
namespace WPFApp
{
public partial class App : Application
{
public App()
{
var container = new ContainerConfiguration()
.WithAssembly(typeof(Exported).Assembly)
.CreateContainer();
MEFLoader.SetContainer(container);
var importer = new Importer();
var importedName = importer.Name;
}
}
}
importedName确实获得了值已导出".在我的生产代码中,我得到了带有详细信息的CompositionFailedException:
importedName does get the value "Exported". In my production code I get a CompositionFailedException with detail:
其他信息:缺少依赖项"UserInformation" "MainWindowViewModel".
Additional information: Missing dependency 'UserInformation' on 'MainWindowViewModel'.
推荐答案
我找到了根本原因.
我的.NET 4.5程序集使用以下命令进入MEF:
My .NET 4.5 assemblies were getting at MEF using:
using System.ComponentModel.Composition;
我的PCL组装件正在使用时:
while my PCL assembles were using:
using System.Composition;
将所有内容更新到System.Composition即可解决问题.
Updating everything to System.Composition solved the problem.
这篇关于NET 4.5和Windows Store PCL的MEF 2合成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!