ComposeParts method not working - c#

I've got an ITagger and an IWpfTextViewMargin, both are exported as MEF components. I want to import the ITagger in my Margin code, and then use some members in that Tagger.
Now I tried to use ComponentContainer in the Margin class, then import the IViewTaggerProvider. I used the following code, which can be found in many MEF tutorials
[Import(typeof(IViewTaggerProvider))]
public IViewTaggerProvider vt_provider { get; set; }
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestMargin).Assembly));
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
System.Diagnostics.Trace.WriteLine(compositionException.Message);
}
and the export code.
[Export(typeof(IViewTaggerProvider))]
[ContentType...
The exported class is defined in another namespace but same assembly.
Here I got problem that ComposeParts(this) throws ImportCardinalityMismatchException. I don't know why the parameter is this. I tried to pass the catalog to it, there is no exception but the import is also null. I also referred to debug mef failures and believe that the exported class has the right contract name and export type identity.
After checking the assembly with Visual MEFx and debugging, I found that probably it's because the IViewTaggerProvider imports a Visual Studio IClassificationTypeRegistryService, which is also an MEF part and results in a rejection of the IViewTaggerProvider.
[Primary Rejection]
[Exception] System.ComponentModel.Composition.ImportCardinalityMismatchException: No valid exports were found that match the constraint '((exportDefinition.ContractName == "Microsoft.VisualStudio.Text.Classification.IClassificationTypeRegistryService") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "Microsoft.VisualStudio.Text.Classification.IClassificationTypeRegistryService".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
So one solution is to add the assembly that exports IClassificationTypeRegistryService. It's a Visual Studio core editor service but I cannot find which assembly exports it. Anyone knows this?
Or any better possible solutions?

Try VisualMEFx. Here is a short blog entry about getting started https://ihadthisideaonce.com/2012/02/22/getting-started-with-visual-mefx/. Once you have it up and running, use VisualMEFx to load the TestMargin assembly and see if any IViewTaggerProvider is exported from that assembly.
Also remember that ImportCardinalityMistmatch doesn't only mean that an export is missing. It can also mean that there are too many exports available that can satisfy the import and MEF has no way of choosing which one to use. So when you are examining your composition in VisualMEFx, check to see if there are too many.
This parameter:
void Bootstrap()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(TestMargin).Assembly));
_container = new CompositionContainer(catalog);
//Fill the imports of this object
try
{
var objectToSatisfy = this;
// var objectToSatifsy = new SomeOtherObjectWithImports();
this._container.ComposeParts(objectToSatisfy);
}
catch (CompositionException compositionException)
{
System.Diagnostics.Trace.WriteLine(compositionException.Message);
}
}
When you call ComposeParts you pass an object to the method. MEF will take the object that you pass and see if there are any imports on it that need to be satisfied. If it finds any imports, it will look in the catalog and try to satisfy them. You can pass any object you want to the ComposeParts method. So I've modified your sample code a little to show two different options. One option is to create some object that needs to be satisfied, and then give it to the container for composition. This is what I have done in the commented out line var objectToSatisfy = new SomeOtherObjectWithImports(). But it is often the case that the object we want to compose is the same object that's calling ComposeParts. So we don't need to create a new object to pass to the container, we already have the object, we just need a reference to it. In C# we can get a reference to the current object instance using the keyword this (in VB.NET the keyword is Me). So, when we want to satisfy imports on the same object that is calling ComposeParts, we can do so by using the this reference as the argument to ComposeParts.
The argument to the ComposeParts method is a parameter array. Informally, this just means that when you write container.ComposeParts(this) it is interpreted as if you had written container.ComposeParts(new object[] { this }). In practice this means you can pass multiple objects to MEF at once, like this:
container.ComposeParts(this, objectToSatifsy, thirdObjectToCompose);
If the object calling ComposeParts has no imports on it, then you should not be using this as the argument. Instead, create an object of the type that you want to compose and pass that to the method. Also, unless all the parts that you want to compose are available in the TestMargin assembly, you need to create more AssemlbyCatalogs for the assemblies that do provide the parts and add them to your AggregateCatalog.

Related

Dynamically compile a class in App_Code while pre-compiling the rest of the project/library

ASP.NET has specicial application folders like App_Code which:
Contains source code for shared classes and business objects (for example, ..cs, and .vb files) that you want to compile as part of your application. In a dynamically compiled Web site project, ASP.NET compiles the code in the App_Code folder on the initial request to your application. Items in this folder are then recompiled when any changes are detected.
Problem is, I'm building a web application, not a dynamically compiled web site. But I'd love to be able to store config values directly in C#, rather than serve via an XML and have to read in during Application_Start and store in HttpContext.Current.Application
So I have the following code in /App_Code/Globals.cs:
namespace AppName.Globals
{
public static class Messages
{
public const string CodeNotFound = "The entered code was not found";
}
}
Which could be anywhere within the application like this:
string msg = AppName.Globals.Messages.CodeNotFound;
The goal is to be able to store any literals in a configurable area that can be updated without recompiling the entire application.
I can use the .cs file by setting its build action to compile, but doing so strips out App_Code/Globals.cs from my output.
Q: Is there a way to identify some parts of a project that should dynamically compile while allowing the rest of the project to be precompiled?
If I set the build action to content - the .cs file will get copied to the bin folder and compiled at runtime. However, in that case, it's not available at design time.
If I set the build action to compile - I can access the objects the same as any other compiled class during design/runtime, but it'll get stripped out of the /App_Code folder when published. I can still place it in the output directory via Copy Always, but the already compiled classes seem to take priority so I can't push config changes without re-deploying the whole application.
Problem Overview
We need to overcome two different problems here:
The first is having a single file that can be compiled at build time and also re-compiled at runtime.
The second is resolving the two different versions of that class created by the solving the first problem so we can actually make use of them.
Problem 1 - Schrödinger's Compilation
The first problem is trying to get a class that is both compiled and not compiled. We need to compile it at design time so that other sections of code are aware it exists and can use its properties with strong typing. But normally, compiled code is stripped out of the output so there aren't multiple versions of the same class causing naming conflicts.
In any case, we need to compile the class initially, but there are two options to persist a re-compilable copy:
Add the file to App_Code, which is compiled at runtime by default, but set it's Build Action = Compile so it's available at design time as well.
Add a regular class file, which is compiled at design time by default, but set it to Copy to Output Directory = Copy Always, so there's a chance we can evaluate it at runtime as well.
Problem 2 - Self Imposed DLL Hell
At a bare minimum, this is a tricky task to charge to the compiler. Any code that consumes a class, must have a guarantee that it exists at compile time. Anything that is dynamically compiled, whether via App_Code or otherwise, will be part of an entirely different assembly. So producing an identical class is treated more like a picture of that class. The underlying type might be the same, but ce n'est une pipe.
We have two options: use an interface or crosswalk between assemblies:
If we use an interface, we can compile it with the initial build and any dynamic types can implement that same interface. This way we are safely relying on something that exists at compile time, and our created class can be safely swapped out as a backing property.
If we cast types across assemblies, it's important to note that any existing usages rely on the type that was originally compiled. So we'll need to grab the values from the dynamic type and apply those property values to the original type.
Existing Answers
Per evk, I like the idea of querying AppDomain.CurrentDomain.GetAssemblies() on startup to check for any new assemblies/classes. I'll admit that using an interface is probably an advisable way to unify precompiled/dynamically compiled classes, but I would ideally like to have a single file/class that can simply be re-read if it changes.
Per S.Deepika, I like the idea of dynamically compiling from a file, but don't want to have to move the values to a separate project.
Ruling out App_Code
App_Code does unlock the ability to build two versions of the same class, but it's actually hard to modify either one after publication as we'll see. Any .cs file located in ~/App_Code/ will be dynamically compiled when the application runs. So in Visual Studio, we can build the same class twice by adding it to App_Code and setting the Build Action to Compile.
Build Action and Copy Output:
When we debug locally, all .cs files will be built into the project assembly and the physical file in ~/App_Code will also be built as well.
We can identify both types like this:
// have to return as object (not T), because we have two different classes
public List<(Assembly asm, object instance, bool isDynamic)> FindLoadedTypes<T>()
{
var matches = from asm in AppDomain.CurrentDomain.GetAssemblies()
from type in asm.GetTypes()
where type.FullName == typeof(T).FullName
select (asm,
instance: Activator.CreateInstance(type),
isDynamic: asm.GetCustomAttribute<GeneratedCodeAttribute>() != null);
return matches.ToList();
}
var loadedTypes = FindLoadedTypes<Apple>();
Compiled and Dynamic Types:
This is really close to solving problem #1. We have access to both types every time the app runs. We can use the compiled version at design time and any changes to the file itself will automatically be recompiled by IIS into a version that we can access at runtime.
The problem is apparent however once we step out of debug mode and try to publish the project. This solution relies on IIS building the App_Code.xxxx assembly dynamically, and that relies on the .cs file being inside the root App_Code folder. However, when a .cs file is compiled, it is automatically stripped out of the published project, to avoid the exact scenario we're trying to create (and delicately manage). If the file was left in, it would produce two identical classes, which would create naming conflicts whenever either one was used.
We can try to force its hand by both compiling the file into the project's assembly and also copying the file into the output directory. But App_Code doesn't work any of it's magic inside of ~/bin/App_Code/. It'll only work at the root level ~/App_Code/
App_Code Compilation Source:
With every publish, we could manually cut and paste the generated App_Code folder from the bin and place it back at the root level, but that's precarious at best. Perhaps we could automate that into build events, but we'll try something else...
Solution
Compile + (Copy to Output and Manually Compile File)
Let's avoid the App_Code folder because it will add some unintended consequences.
Just create a new folder named Config and add a class that will store the values we want to be able to modify dynamically:
~/Config/AppleValues.cs:
public class Apple
{
public string StemColor { get; set; } = "Brown";
public string LeafColor { get; set; } = "Green";
public string BodyColor { get; set; } = "Red";
}
Again, we'll want to go to the file properties (F4) and set to compile AND copy to output. This will give us a second version of the file we can use later.
We'll consume this class by using it within a static class that exposes the values from anywhere. This helps separate concerns, especially between the need to dynamically compile and statically access.
~/Config/GlobalConfig.cs:
public static class Global
{
// static constructor
static Global()
{
// sub out static property value
// TODO magic happens here - read in file, compile, and assign new values
Apple = new Apple();
}
public static Apple Apple { get; set; }
}
And we can use it like this:
var x = Global.Apple.BodyColor;
What we'll attempt to do inside the static constructor, is seed Apple with the values from our dynamic class. This method will be called once every time the application is restarted, and any changes to the bin folder will automatically trigger recycling the app pool.
In short order, here's what we'll want to accomplish inside of the constructor:
string fileName = HostingEnvironment.MapPath("~/bin/Config/AppleValues.cs");
var dynamicAsm = Utilities.BuildFileIntoAssembly(fileName);
var dynamicApple = Utilities.GetTypeFromAssembly(dynamicAsm, typeof(Apple).FullName);
var precompApple = new Apple();
var updatedApple = Utilities.CopyProperties(dynamicApple, precompApple);
// set static property
Apple = updatedApple;
fileName - The File path might be specific to where you'd like to deploy this, but note that inside of a static method, you need to use HostingEnvironment.MapPath instead of Server.MapPath
BuildFileIntoAssembly - In terms of loading the assembly from a file, I've adapted the code from the docs on CSharpCodeProvider and this question on How to load a class from a .cs file. Also, rather than fight dependencies, I just gave the compiler access to every assembly that was currently in the App Domain, same as it would have gotten on the original compilation. There's probably a way to do that with less overhead, but it's a one time cost so who cares.
CopyProperties - To map the new properties onto the old object, I've adapted the method in this question on how to Apply properties values from one object to another of the same type automatically? which will use reflection to break down both objects and iterate over each property.
Utilities.cs
Here's the full source code for the Utility methods from above
public static class Utilities
{
/// <summary>
/// Build File Into Assembly
/// </summary>
/// <param name="sourceName"></param>
/// <returns>https://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx</returns>
public static Assembly BuildFileIntoAssembly(String fileName)
{
if (!File.Exists(fileName))
throw new FileNotFoundException($"File '{fileName}' does not exist");
// Select the code provider based on the input file extension
FileInfo sourceFile = new FileInfo(fileName);
string providerName = sourceFile.Extension.ToUpper() == ".CS" ? "CSharp" :
sourceFile.Extension.ToUpper() == ".VB" ? "VisualBasic" : "";
if (providerName == "")
throw new ArgumentException("Source file must have a .cs or .vb extension");
CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);
CompilerParameters cp = new CompilerParameters();
// just add every currently loaded assembly:
// https://stackoverflow.com/a/1020547/1366033
var assemblies = from asm in AppDomain.CurrentDomain.GetAssemblies()
where !asm.IsDynamic
select asm.Location;
cp.ReferencedAssemblies.AddRange(assemblies.ToArray());
cp.GenerateExecutable = false; // Generate a class library
cp.GenerateInMemory = true; // Don't Save the assembly as a physical file.
cp.TreatWarningsAsErrors = false; // Set whether to treat all warnings as errors.
// Invoke compilation of the source file.
CompilerResults cr = provider.CompileAssemblyFromFile(cp, fileName);
if (cr.Errors.Count > 0)
throw new Exception("Errors compiling {0}. " +
string.Join(";", cr.Errors.Cast<CompilerError>().Select(x => x.ToString())));
return cr.CompiledAssembly;
}
// have to use FullName not full equality because different classes that look the same
public static object GetTypeFromAssembly(Assembly asm, String typeName)
{
var inst = from type in asm.GetTypes()
where type.FullName == typeName
select Activator.CreateInstance(type);
return inst.First();
}
/// <summary>
/// Extension for 'Object' that copies the properties to a destination object.
/// </summary>
/// <param name="source">The source</param>
/// <param name="target">The target</param>
/// <remarks>
/// https://stackoverflow.com/q/930433/1366033
/// </remarks>
public static T2 CopyProperties<T1, T2>(T1 source, T2 target)
{
// If any this null throw an exception
if (source == null || target == null)
throw new ArgumentNullException("Source or/and Destination Objects are null");
// Getting the Types of the objects
Type typeTar = target.GetType();
Type typeSrc = source.GetType();
// Collect all the valid properties to map
var results = from srcProp in typeSrc.GetProperties()
let targetProperty = typeTar.GetProperty(srcProp.Name)
where srcProp.CanRead
&& targetProperty != null
&& (targetProperty.GetSetMethod(true) != null && !targetProperty.GetSetMethod(true).IsPrivate)
&& (targetProperty.GetSetMethod().Attributes & MethodAttributes.Static) == 0
&& targetProperty.PropertyType.IsAssignableFrom(srcProp.PropertyType)
select (sourceProperty: srcProp, targetProperty: targetProperty);
//map the properties
foreach (var props in results)
{
props.targetProperty.SetValue(target, props.sourceProperty.GetValue(source, null), null);
}
return target;
}
}
But Why Tho?
Okay, so there are other more conventional ways to accomplish the same goal. Ideally, we'd shoot for Convention > Configuration. But this provides the absolute easiest, most flexible, strongly typed way to store config values I've ever seen.
Normally config values are read in via an XML in an equally odd process that relies on magic strings and weak typing. We have to call MapPath to get to the store of value and then do Object Relational Mapping from XML to C#. Instead here, we have the final type from the get go, and we can automate all of the ORM work between identical classes that just happen to be compiled against different assemblies.
In either case, the dream output of that process is to be able to write and consume C# directly. In this case, if I want to add an extra, fully configurable property, it's as easy as adding a property to the class. Done!
It will be available immediately and recompiled automatically if that value changes without needing to publish a new build of the app.
Dynamically Changing Class Demo:
Here's the full, working source code for the project:
Compiled Config - Github Source Code | Download Link
You can move configuration part to separate project, and create common interface like (IApplicationConfiguration.ReadConfiguration) to access it.
You can compile the code dynamically at run time like below, and you can access the configuration details using reflection.
public static Assembly CompileAssembly(string[] sourceFiles, string outputAssemblyPath)
{
var codeProvider = new CSharpCodeProvider();
var compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = false,
IncludeDebugInformation = true,
OutputAssembly = outputAssemblyPath
};
// Add CSharpSimpleScripting.exe as a reference to Scripts.dll to expose interfaces
compilerParameters.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
var result = codeProvider.CompileAssemblyFromFile(compilerParameters, sourceFiles); // Compile
return result.CompiledAssembly;
}
Let's see how dynamic compilation of files in App_Code works. When first request to your application arrives, asp.net will compile code files in that folder into assembly (if were not compiled before), and then load that assembly into current application domain of asp.net application. That's why you see your message in a watch - assembly was compiled and is available in current app domain. Because it was compiled dynamically, of course you have compile-time error when trying to reference it explicitly - this code is not yet compiled, and when it will be compiled - it might have completely different structure and message you reference might just not be there at all. So there is no way you can explicitly reference code from dynamic-generated assembly.
What options do you have then? For example, you can have an interface for your messages:
// this interface is located in your main application code,
// not in App_Code folder
public interface IMessages {
string CodeNotFound { get; }
}
Then, in your App_Code file - implement that interface:
// this is in App_Code folder,
// you can reference code from main application here,
// such as IMessages interface
public class Messages : IMessages {
public string CodeNotFound
{
get { return "The entered code was not found"; }
}
}
And then in main application - provide a proxy by searching current app domain for assembly with type that implements IMessage interface (only once, then cache it) and proxy all calls to that type:
public static class Messages {
// Lazy - search of app domain will be performed only on first call
private static readonly Lazy<IMessages> _messages = new Lazy<IMessages>(FindMessagesType, true);
private static IMessages FindMessagesType() {
// search all types in current app domain
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var type in asm.GetTypes()) {
if (type.GetInterfaces().Any(c => c == typeof(IMessages))) {
return (IMessages) Activator.CreateInstance(type);
}
}
}
throw new Exception("No implementations of IMessages interface were found");
}
// proxy to found instance
public static string CodeNotFound => _messages.Value.CodeNotFound;
}
This will achieve your goal - now when you change code in App_Code Messages class, on next request asp.net will tear down current application domain (first waiting for all pending requests to finish), then create new app domain, recompile your Messages and load into that new app domain (note that this recreating of app domain always happen when you change something in App_Code, not just in this particular situation). So next request will already see new value of your message without you explicitly recompile anything.
Note that you obviously cannot add or remove messages (or change their names) without recompiling main application, because doing that will require changes to IMessages interface which belongs to main application code. If you try - asp.net will throw compilation failure error on next (and all subsequent) requests.
I'd personally avoid doing such things, but if you are fine with that - why not.

Decide what dll to call at runtime

I am using the Process.Start() method to launch a specific executable as needed. The executable is tailor-made, there are many of these, and the paths & names are in the database together with the job they do.
Is there another way to launch them in-process, if they all implement an interface? I want to be able to debug them and have better type safety when calling them.
I can set a library project in each solution, then instantiate that library and have it do the job instead of the executable. The question is about a mechanism that would allow me to build against the interface then load the library by its name at runtime.
You can use something like the following if you want custom solution. Otherwise, you can use modular patterns implemented in Prism with Unity or MEF.
public interface IPlugin
{
void Start();
}
// You can change this to support loading based on dll file name
// Use one of the other available Assembly load options
public void Load(string fullAssemblyName)
{
var assembly = System.Reflection.Assembly.Load(fullAssemblyName);
// Assuming only one class implements IPlugin
var pluginType = assembly.GetTypes()
.FirstOrDefault(t => t.GetInterfaces()
.Any(i=> i == typeof(IPlugin)));
// Concrete class implementing IPlugin must have default empty constructor
// for following instance creation to work
var plugin = Activator.CreateInstance(pluginType) as IPlugin;
plugin.Start();
}

How to use RegisterType(Type).As(Type)

My MVC project uses Autofac 3.0 and I want to RegisterType by reflection.
First defined one interface and implementation named like IRegisterDemo and RegisterDemo less parameters.
I tried to use builder.RegisterType(typeof(RegisterDemo)).As(typeof(IRegisterDemo)) in my Application_Start method, it successed, but it's not my purpose.
I want to define one attribute like UseAutofacAttribute to reflect the types and use RegisterType(ImplType).As(InterfaceType), when I wrote the code in Application_Start, it worked, so I built another project to do this further and referenced the project dll in my MVC project, and in Application_Start just run one static method, unfortunately it failed.
So I want to know the reason and how to change?
If you want to make your registrations attribute based, I'd suggest you use Autofac's MEF adapter. It will let you decorate your types with the ExportAttribute and be picked up by your ContainerBuilder.
Example
[Export(typeof(IRegisterDemo))] // <-- This exports RegisterDemo as IRegisterDemo
public class RegisterDemo : IRegisterDemo { }
// Example method to register assemblies with ContainerBuilder
public void RegisterPartsFromReferencedAssemblies(ContainerBuilder builder)
{
// Get referenced assemblies
var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
// Create an AssemblyCatalog from each assembly
var assemblyCatalogs = assemblies.Select(x => new AssemblyCatalog(x));
// Combine all AssemblyCatalogs into an AggregateCatalog
var catalog = new AggregateCatalog(assemblyCatalogs);
// Register the catalog with the ContainerBuilder
builder.RegisterComposablePartCatalog(catalog);
}
This example shows how to export a type and how to register referenced assemblies with the ContainerBuilder. You can then do container.Resolve<IRegisterDemo>() to get the exported part (or have it injected into a class).
NOTE: This will register exported parts from all referenced assemblies, including:
Assemblies specified in the assemblies element of the Web.config file
Assemblies built from custom code in the App_Code directory
Assemblies in other top-level folders

Can't load multiple MEF parts

I have a Winforms desktop application that is loading multiple MEF parts with the same Interface type.
Problem:
When I try to load more than one of the same type I get the following exception:
The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No valid exports were found that match the constraint '((exportDefinition.ContractName = "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "BOCA.TaskPilot.Common.Extensions.IFolderViewExtension".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
Resulting in: Cannot set import 'TaskPilot.Windows.MainForm.FolderViewExtension (ContractName="BOCA.TaskPilot.Common.Extensions.IFolderViewExtension")' on part 'TaskPilot.Windows.MainForm'.
Element: TaskPilot.Windows.MainForm.FolderViewExtension (ContractName="BOCA.TaskPilot.Common.Extensions.IFolderViewExtension") --> TaskPilot.Windows.MainForm
Here is the code to load the parts:
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
//string myExecName = Assembly.GetExecutingAssembly().Location;
//string myPath = Path.GetDirectoryName(myExecName);
catalog.Catalogs.Add(new DirectoryCatalog(#"C:\Data\TaskPilot\Development\Source\BOCA.TaskPilot.FolderView\bin\Debug"));
catalog.Catalogs.Add(new DirectoryCatalog(#"C:\Data\TaskPilot\Development\Source\BOCA.TaskPilot.TaskView\bin\Debug"));
// Uncomment below line and it works without exceptions raised
//catalog.Catalogs.Add(new DirectoryCatalog(#"C:\Data\TaskPilot\Development\Source\BOCA.FileManager\bin\Debug"));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
Here's the code at the class for each of the MEF parts:
[Export(typeof(IFolderItemsViewExtension))
public partial class TaskTreeView : DevExpress.XtraEditors.XtraUserControl, IFolderItemsViewExtension, IPartImportsSatisfiedNotification]
Here's the Import used on the Main form:
[ImportMany(AllowRecomposition = true)]
private IEnumerable<IFolderItemsViewExtension> TaskViewExtensions = null;
If I uncomment the last Catalog.Catalogs.Add line it throws the exception. If I run it without that it runs just fine. That line loads a different user control that implements the IFolderItemsViewExtension Interface. I've tried to just load a dummy project that all it has is the user control and that interface and I still get the same exception. No matter what I do I still get this exception.
It seems that everything runs fine as long as I'm not loading more than one of the same type of MEF part export.
This is using the latest version of 2009.22.10.0 of the System.ComponentModel.Composistion from the MEF download.
The error indicates that it can't find an export of type IFolderViewExtension. Note that this is different from the import of IFolderItemsViewExtension you have shown.
My guess is that the problem is not that you have multiple IFolderItemsViewExtensions, but that you have multiple IFolderViewExtensions, or there is some other contract that you have more than one of that you are using with an import that requires exactly one.
This may be caused because you have the same assembly in more than one of your directory catalogs. It is easy for this to happen if you have a reference to an assembly and copy local is set to true.
I guess might have more than one export statement in your Export class.
I was facing the same issue and this solved when i removed all other expert statement from that export class. and now it is working fine.

Type.IsSubclassOf() doesn't work across AppDomains?

I'm having some problems with the following code:
private class ClientPluginLoader : MarshalByRefObject
{
public bool IsPluginAssembly(string filename)
{
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += new ResolveEventHandler(CurrentDomainReflectionOnlyAssemblyResolve);
Assembly asm = Assembly.ReflectionOnlyLoadFrom(filename);
Type[] types = asm.GetTypes();
foreach (Type type in types)
{
if (type.IsSubclassOf(typeof(ClientPlugin)))
{
return true;
}
}
return false;
}
}
The code is called via a proxy that I've created through my custom app domain's CreateInstanceFromAndUnwrap(). This means that IsPluginAssembly() is executed in the context of my custom app domain.
The problem is that the call to IsSubclassOf() always returns false, even though it should IMHO return true. The "type" in question really does inherit from ClientPlugin - there's no doubt about that.
ClientPlugin is defined in a different private assembly, which I'm resolving manually, as evident in the code fragment above.
I've put a breakpoint on the if (type.IsSubclassOf(...)) line and confirmed this expression to be false:
type.BaseType == typeof(ClientPlugin)
On the other hand, this expression is true:
type.BaseType.FullName == typeof(ClientPlugin).FullName
How is this possible? What's going on?
UPDATE: Kent Boogaart pointed me to the right direction. I searched the web a bit more and run into this blog post. It seems I'll have to resolve my Load/LoadFrom/ReflectionOnlyLoadFrom conflicts in order to make this work.
This is due to loading into a different context. How you load an assembly (Load / LoadFrom / ReflectionOnlyLoad) determines which context it is loaded into. This simple example also demonstrates the problem:
using System;
using System.Reflection;
class Foo
{
public static void Main()
{
var type = typeof(Foo);
var reflectionLoadType = Assembly.ReflectionOnlyLoad("ConsoleApplication1").GetType("Foo");
Console.WriteLine(type == reflectionLoadType); //false
Console.WriteLine(type.Equals(reflectionLoadType)); //false
Console.WriteLine("DONE");
Console.ReadKey();
}
}
See here for more info.
I've had a similar problem. I also had this architecture - a .DLL that contains ClientPlugin base class; several plugins which reference this .DLL; and a main application, which also references this .DLL. The problem was that the .DLL with the ClientPlugin base class was copied in two folders - both the Plugins folder, and the main application folder. Thus it got loaded twice in my AppDomain (plugins also loaded it indirectly). And when the main application tried to do reflection type magic, it failed, because there were two instances of the ClientPlugin type.
Although I don't think this is exactly your case, there still is a lesson to learn here - if a .DLL gets loaded twice, the types will also be duplicated. In your case I would suspect either separate AppDomains, or the "ReflectionOnlyLoad", because the .DLL is then loaded somehow differently.

Categories

Resources