Why does ComposeExportedValue not insert my object into the container?

534 views Asked by At

I have the following line of code that seems to do nothing at all, no errors, just does nothing. The object is not in the container's catalog:

public void Initialize()
{
  if (AuthenticationModel.Current.Protocol.ToString() != GetType().Name.Replace(UnitTypeSuffix, ""))
  {
    return;
  }
  _container.ComposeExportedValue<ILoginView>(new LoginView());
}

The if check is because I potentially have more than one login module, and only the one specified by Protocol must make it's view available in the container. Then other dynamically loaded modules can just ask for the login view currently registered. The check passes and ComposeExportedValue executes, but the object is not visible in the container.

1

There are 1 answers

1
Markus Luedin On BEST ANSWER

If you do something like this, you will see that it does use your login view, when it composes the test object:

public class Test
{
    [Import]
    public ILoginView LoginView { get; set; }
}

var test = new Test();
container.ComposeExportedValue<ILoginView>(new LoginView());
container.SatisfyImportsOnce(test);

The documentation is not very detailed, but I think it does not get added to the container because you created the object, and you keep the ownership of it. It does not get disposed when the container gets disposed etc.

If you want it to be part of the container you have to use either attributes [Export] and add the assembly that contains your view to the catalog. Or if you don't want to use attributes, you can use the new RegistrationBuilder.