mef

MEF: difference between GetExportedValue and SatisfyImports

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 15:41:24
We're using MEF (.NET 4, can't use 4.5 at the moment) in a MVVM application. Everything was fine until we needed to create models on the fly, for instance editable rows of a table. I didn't want to run into memory leaks, I found this article http://pglazkov.blogspot.ch/2011/04/mvvm-with-mef-viewmodelfactory.html and I discovered an unexpected behavior that I would like to understand. This is an Item added to the Shell.Items observable collection: [PartCreationPolicy(CreationPolicy.NonShared)] [Export] public class Item : INotifyPropertyChanged, IDisposable { [Import] private Lazy<Shell> shell;

Is it possible to parameterize a MEF import?

我的梦境 提交于 2019-12-05 13:52:08
I am relatively new to MEF so I don't fully understand the capabilities. I'm trying to achieve something similar to Unity's InjectionMember. Let's say I have a class that imports MEF parts. For the sake of simplicity, let's take the following class as an example of the exported part. [Export] [PartCreationPolicy(CreationPolicy.NonShared)] public class Logger { public string Category { get; set; } public void Write(string text) { } } public class MyViewModel { [Import] public Logger Log { get; set; } } Now what I'm trying to figure out is if it's possible to specify a value for the Category

MEF Generic Imports

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 12:20:37
I have the following example code using MEF: public interface IFoo<T> {} public class Foo<T> : IFoo<T> {} [Export(typeof(IFoo<String>))] public class Foo : Foo<String> {} public class Bar<T> { [Import] private readonly IFoo<T> foo; } static void Main() { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); var container = new CompositionContainer(catalog); container.ComposeParts(); var bar = new Bar<String>(); //bar.foo would be null } This doesn't seem to work - the foo field is null . Is this because its type isn't seen by MEF as

MEF ComposeParts. How to handle plugin exceptions

强颜欢笑 提交于 2019-12-05 11:54:36
I have searched on the web for a solution, but I didn't find anything. In my C# application I am using MEF for implementing a plugin pattern. Everything is working fine. However today I have tried to figure out what happens if a Plugin Constructor throws an Exception for some reason. To load plugins I am using CompositionContainer.ComposeParts . If for some reason one of the X plugins throws an exception this method will fail and nothing will be loaded. Is there a way to just catch the single exception, log it and continue? Thank you in advance. I'm guessing you're calling CompositionContainer

How to sort views in an ItemsControl in Prism / MEF?

给你一囗甜甜゛ 提交于 2019-12-05 10:48:01
I use prism v4 and MEF to load my modules. My modules contain a handful of views (MVVM) which are loaded in a ItemsControl/NavigationRegion automatically by MEF. This works nicely, all items show up in the ItemControl. But I don't like the order in which they show. One module might contain several of the items, so changing the module load order is not enough by itself. How can I sort the different views in the ItemsControl? Is there any way to sort them by some property? I use prism V4, MEF and exploration due to attributes like in the StockTraderRI example. This is actually baked into Prism4.

How to properly scope composition per request using ASP.NET MVC, WebAPI, and MEF

被刻印的时光 ゝ 提交于 2019-12-05 09:42:35
I recently added MEF to an MVC/WebAPI application using a variety of resources including this SO answer How to integrate MEF with ASP.NET MVC 4 and ASP.NET Web API . While this worked for a time, I started to receive intermittent errors related to making connections to the database, the most frequent one being: "System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached." I realized I was leaking connections but didn't understand why.

MEF初步使用

时间秒杀一切 提交于 2019-12-05 09:25:58
MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库。 应用程序开发人员可利用该库发现并使用扩展,而无需进行配置。 扩展开发人员还可以利用该库轻松地封装代码,避免生成脆弱的硬依赖项。 通过 MEF,不仅可以在应用程序内重用扩展,还可以在应用程序之间重用扩展。(摘自MSDN) 我初步使用后的感受是通过这个应用程序库,我们可以实现接口功能的扩展和自动导入到实例,不需要自己编码对接口进行实现。它可以在指定的程序集中搜索匹配合适的接口实现,然后导入使用。 弄了一个控制台程序,代码不多,很简单,我就直接上代码了。看完了应该会有一个初步认知。 文件一:program.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.Reflection; namespace MEFdemo { class Program { public IbookService

How to release a shared instance from a MEF container

落爺英雄遲暮 提交于 2019-12-05 09:04:50
I am trying to release a shared instance or singleton value. Does anyone know how to do this? Do I have to refresh the catalog? I'm learning MEF so please help. Example of class [Export] public class Foo { public RandomProperty {get;set;} [ImportConstructor] public Foo() {} } You can create it with something like this: var fooSingleton = ServiceLocator.GetInstance(typeof(Foo)); All fine and good, but ideally I would like to do something like this Container.Replace(oldFoo, newFoo); So when I call it again var fooSingleton = ServiceLocator.GetInstance(typeof(Foo)); fooSingleton will have the new

Decoupling the view, presentation and ASP.NET Web Forms

孤人 提交于 2019-12-05 07:05:29
I have an ASP.NET Web Forms page which the presenter needs to populate with controls. This interaction is somewhat sensitive to the page-life cycle and I was wondering if there's a trick to it, that I don't know about. I wanna be practical about the whole thing but not compromise testability. Currently I have this: public interface ISomeContract { void InstantiateIn(System.Web.UI.Control container); } This contract has a dependency on System.Web.UI.Control and I need that to be able to do things with the ASP.NET Web Forms programming model. But neither the view nor the presenter may have

Prism: Share data between ViewModels loaded into a region?

做~自己de王妃 提交于 2019-12-05 01:47:53
I've got a View containing a TabControl bound to a region "CustomerRegion". I've got a few View/ViewModels which are marked as [ViewExport(RegionName = "CustomerRegion")]. These items are loaded successfully into the TabControl. But now I need these TabItems to share some data in this TabControl (can't be global since there might be two or more of these open at one time). Any ideas how these TabItems can share data without leaving MVVM/MEF? Prism has the concept of "Region Context" that you might be able to use, depending on your scenario. Check out the Prism Region documentation and look for