Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

Commented Issue: If a base class has an import that fails the exception does not give the right reason [8492]

$
0
0
In the code below the test fails until you un-comment on the line "var typeCatalog = new TypeCatalog(typeof(Foo));//, typeof(Bar));". Then the test works.

FooImporter is a class that imports Foo. The Foo has a base class FooBase. FooBase has an import on IBar. The contract IBar is not possible to satisfy without the type Bar that exports the contract IBar.

If I try to satisfy imports on Foo without the container knowing Bar the error reported is not that an IBar Export is missing. The error is that IFoo is missing which it technically is not. It is present but has a child that is missing.

On the other hand if you try to satisfy imports on the Foo class direct the error shows that IBar is missing (second test).

As I see it there is a break in the error reporting causing the erroneous assumption that IFoo is missing when it is IBar that is missing. But only when Foo is not the root of the graph satisfied by MEF.

Cheers,

M.

[TestClass]
public class MEFBadBaseImportTests
{
[TestMethod]
public void Bad_base_class_messes_up_the_error_message()
{
var typeCatalog = new TypeCatalog(typeof(Foo));//, typeof(Bar));
var compositionContainer = new CompositionContainer(typeCatalog);

var fooImporter = new FooImporter();

try
{
compositionContainer.SatisfyImportsOnce(fooImporter);
}
catch (CompositionException ce)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Message:").AppendLine(ce.Message);
stringBuilder.AppendLine(new string('*', 42));
foreach (var error in ce.Errors)
{
stringBuilder.AppendLine("Error:").AppendLine(error.Exception.Message);
stringBuilder.AppendLine(new string('*', 42));
}
Assert.Fail(stringBuilder.ToString());
}

Assert.IsNotNull(fooImporter.Foo);
}

[TestMethod]
public void Using_the_class_direct_shows_the_correct_error()
{
var typeCatalog = new TypeCatalog(typeof(Foo));//, typeof(Bar));
var compositionContainer = new CompositionContainer(typeCatalog);

var foo = new Foo();

try
{
compositionContainer.SatisfyImportsOnce(foo);
}
catch (CompositionException ce)
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Message:").AppendLine(ce.Message);
stringBuilder.AppendLine(new string('*', 42));
foreach (var error in ce.Errors)
{
stringBuilder.AppendLine("Error:").AppendLine(error.Exception.Message);
stringBuilder.AppendLine(new string('*', 42));
}
Assert.Fail(stringBuilder.ToString());
}

Assert.IsNotNull(foo.Bar);
}

public interface IFoo { }

public interface IBar { }

public abstract class FooBase : IFoo
{
[Import]
public IBar Bar { get; set; }
}

[Export(typeof(IFoo))]
public class Foo : FooBase
{ }

[Export(typeof(IBar))]
public class Bar : IBar { }

public class FooImporter
{
[Import]
public IFoo Foo { get; set; }
}
}
Comments: I ran into the same issue. Very misleading message....

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>