首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何初始化MEF ServiceLocator.Current?

如何初始化MEF ServiceLocator.Current?
EN

Stack Overflow用户
提问于 2012-01-10 11:33:59
回答 2查看 6.2K关注 0票数 3

在我的App.xaml.cs中

代码语言:javascript
复制
private void InitializeContainer()
{
    var catalogs = new AggregateCatalog();

    var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
    catalogs.Catalogs.Add(catalog);

    // Also adding Interactions project
    catalog = new AssemblyCatalog(typeof(InteractionsService).Assembly);
    catalogs.Catalogs.Add(catalog);

    // initialize the main application composition host (container) 
    CompositionHost.Initialize(catalogs);
} 

但是,当我尝试初始化对象时,如下所示:

代码语言:javascript
复制
this.InteractionsService = ServiceLocator.Current.GetInstance<IInteractionsService>();

我得到了异常,我的ServiceLocator.Current为空。

我该如何让它工作呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-10 13:55:51

我想您在设置CompositionContainer时错过了对ComposePartsCompose的调用。在您开始通过GetInstance解析实例之前。

这里有一个MSDN walk through here,以及其他一些herehere示例。相关的代码片段如下:

代码语言:javascript
复制
private CompositionContainer _container

//your code

var batch = new CompositionBatch();
batch.AddPart(this);

_container.Compose(batch);

//or more simply
_container.ComposeParts(this) //if this method is supported
票数 1
EN

Stack Overflow用户

发布于 2012-04-26 02:24:52

我也有同样的问题..

找到了这个可能有帮助的东西,

http://www.itwox.com/post/Using-MEF-with-Common-Service-Locator.aspx

关键语句是

ServiceLocator.SetLocatorProvider(() => whateveryourlocatorinstanceis );

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8798221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档