Issue with ComposeExportedValue

Nov 11, 2010 at 2:02 PM


I have a 2 simple silverlight applications hosted under 2 different virtual directories. The Main application has a ContentControl inside which I am exporting the Second applications xap using an DeploymentCatalog.

In the second app (which exports itself as typeof(UserControl)) I have a property with the import tag on it as follows

[Import("Prop1", AllowDefaults = true, AllowRecomposition = true)]
public int Prop1 {get; set;}

Now from the Main application I download the xap and then I call the ComposeExportedValue as below

            CatalogService.GetContainer().ComposeExportedValue("Prop1", 123);

It downloads the xap perfectly in the content control and it sets the value of Prop1 in it to 123.

Now I need to be able to change the value of Prop1 from the Main application. For that I have a button in the Main application, clicking on which it calls this

CatalogService.GetContainer().ComposeExportedValue("Prop1", 456);

Now what's happening is Prop1 seems to be getting updated as 0 instead of 456 with no errors whatsoever. Is this not permitted in MEF? I had to work around it by using dynamic variable and the setting the Prop1 value straight by obtaining the exported UserControl from the CompositionContainer.

I want to avoid this. Is there a better way to handle this? Would this involve getting rid of the xap and downloading it again and passing the new value?





Nov 11, 2010 at 8:05 PM

When you called ComposeExportedValue("Prop1", 456) you didn't replace the "Prop1" contact you actually added another instance of the "Prop1" contract. Your Prop1 property was set to 0 because you enabled AllowDefault=true and in that case if more than on exists it sets it to default instead of one of the values.

Honestly the easiest way to do what you want is to introduce one more layer of indirection. Something like:

public class Prop1Container {
    public int Prop1 { get; set; }

Now for the one that wants that value they can Import Prop1Container and get the value and for the one that wants to change they can Import it and set the value.

Nov 12, 2010 at 4:22 AM

So this effectively means sharing the Prop1Container between the two applications?

Nov 12, 2010 at 4:32 AM

Actually I feel the going the dynamic way makes sense. What I had done was this

dynamic export = CatalogService.GetContainer().GetExportedValue<UserControl>("ExternalModule");
export.Prop1 = 456;


The above seems to work well. Would you recommend this?