mef

MEF - [ImportMany] using ExportFactory<T> in WPF - .NET 4.0

强颜欢笑 提交于 2019-12-07 15:17:39
I have some part imports that I need to create multiple instances of. By searching around I decided I needed to use the ExportFactory class. Unfortunately, the ExportFactory class is not available in WPF by default, but luckily Glenn Block has ported the code . Originally, I was specifying the type when importing: [ImportMany(typeof(IMyModule))] public IEnumerable<Lazy<IMyModule, IMyModuleMetadata>> Modules { get; set; } I also created an export attribute: [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] public class ExportMyModuleMetadata : ExportAttribute,

MEF: GetExportedValue from Type?

别来无恙 提交于 2019-12-07 14:56:11
问题 Using MEF I can create and load a type like this: var view = Container.GetExportedValue<MyView>(); Now what I want to do is this: Type t = typeof(MyView); var view = Container.GetExportedValue<t>(); (of course the type might contain something different than MyView). This is not possible using the generics GetExportedValue<> - is there some other way to achieve this? 回答1: You can use reflection. Here is an example: using System; using System.ComponentModel.Composition; using System

Is it safe to store an ObjectContext in a thread static variable in ASP.NET?

眉间皱痕 提交于 2019-12-07 14:00:05
问题 I've read that I should store an ObjectContext in HttpContext.Current in order to share my ObjectContext across different services/repositories that are called in a request. I'm wondering if it is safe to use an ObjectContext with the [ThreadStatic] attribute on a static class variable instead. Is that a safe thing to do? Is each request processed in its own thread? 回答1: No, there can be multiple requests in the same thread and, more importantly, one request can be processed in multiple

MEF Generic Imports

感情迁移 提交于 2019-12-07 08:45:41
问题 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

MEF, why are identical duplicates of one and the same exported plugin created?

拟墨画扇 提交于 2019-12-07 08:35:33
问题 (1) Using the code below I get exactly 2 items in my containers of one and the same exported plugin and I wonder why: (2) Additional question which I really cannot implement: How can I extend the framework to handle different plugin types (such as having several imports of different types, or one import that stores all plugins in a dynamic IEnumerable or so). I want to provide in my static wrapper class one generic method that returns the discovered plugin as a function of type and matching

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

回眸只為那壹抹淺笑 提交于 2019-12-07 06:02:46
问题 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

What's the recommended way to run plugins with dependency dll's that have different version?

纵然是瞬间 提交于 2019-12-07 06:02:43
问题 I have a WCF plugin service that loads plugins via MEF and runs them. Each plugin is a directory with multiple dll's that implements a specific interface. I load all the plugins in the same AppDomain using MEF ( DirectoryCatalog ) and run them in a generic way (using reflection). Now lets assume I have two plugins with dll dependencies: Plugin1 ----> Entities 1.0.0.1 Plugin2 ----> Entities 1.0.0.2 I've added some new entities in 1.0.0.2. When I run the plugin's I get a sporadic error that the

How-To Prevent Module Duplicates with MEF?

梦想的初衷 提交于 2019-12-07 05:06:00
问题 How can i prevent from MEF to load duplicates Modules in the case of the presence of 2 copies of the same Assembly (maybe by mistake) Assembly1.dll Assembly2.dll (copy of Assembly1) [ImportMany] public IList<IModule> Modules { get; private set; } public void BuildUp() { Modules = new List<IModule>(); var catalog = new DirectoryCatalog(@".\Modules"); var container = new CompositionContainer(catalog); container.ComposeParts(this); } 回答1: Instead of using a DirectoryCatalog, use an

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

心已入冬 提交于 2019-12-07 04:53:05
问题 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

GetExportedValue cannot be called before prerequisite import has been set

旧时模样 提交于 2019-12-07 02:55:04
问题 We are using MEF in a WPF application. And we are getting this error : GetExportedValue cannot be called before prerequisite import 'MyNamespace.MyMainClass..ctor (Parameter="myParameter", ContractName="IContractInterface")' has been set. I am not using threads , I read sometimes this error happens with multiple threads, however just in case I created the composition container passing the "thread safe" parameter in true var container = new CompositionContainer(catalog, true); The Code I have