.net - Exporting / Importing external types in MEF - Silverlight -


i'm working on implementing dynamic loading of pages existing silverlight application. far, good. both importing pages external .xap's work , exporting data host application plugins working.

the host application consumes webservice exposes number of types relating backend system. question how export objects of types defined in webservice?

an example export list of sms_supportedplatforms, defined in ws client. hosting application. property lives in app.xaml.cs of main silverlight application. filled async call webservice.

 [export(exportcontracts.sms_supportedplatforms)]     public static list<client.sms_supportedplatforms> supportedplatforms = new list<client.sms_supportedplatforms>();  

edited: i've moved imports seperate class in external assemblies. recide in class:

public class neoservicemanagerimports {         [import(neosmexportcontracts.departmentexportattribute, allowrecomposition = true)]         public string department { get; set; }          [import(neosmexportcontracts.rolesexportattribute, allowrecomposition = true)]         public list<string> roles { get; set; }            [import(neosmexportcontracts.sms_supportedplatforms, allowrecomposition = true)]         public list<client.sms_supportedplatforms> supportedplatforms         {             get;             set;         }          public neoservicemanagerimports()         {             compositioninitializer.satisfyimports(this);         } 

}

this class called constructor of page exported (for testing purposes). page exported metadataattribute uiproviderbase class i've bade exporting plugins(ignore abstract naming ;-)) class has couple of props logo, title, , list of pages.

[export(typeof(uiproviderbase))]     public class externalmainmenuexternalsubmenuuiprovider: uiproviderbase     {          public override string title         {             { return "submenu"; }         }          public override string imageuri         {             { return "uriuri"; }         }          [importmany("externalsubmenuforexternalmainmenucontract")]         public override list<system.componentmodel.composition.exportfactory<frameworkelement, ipagemetadata>> entrypage         {             get;             set;         }     } 

i sure issue relates mef being unable resolve type same, when being referenced 2 different assemblies. there way solve this, without refactoring hosting application take list of isms_supportedplatforms? seems hosting application exporting correctly, never discovered in plugin.

if have allowdefault = true, page loaded supportedplatforms stays null. if false, doesn't export page , fails silently.

i've changed way load in pages bit, , i'm trying more info you. error see now

the composition produced single composition error. root cause provided below. review compositionexception.errors property more detailed information.  1) no valid exports found match constraint '((exportdefinition.contractname == "sms_supportplatformsexport") andalso (exportdefinition.metadata.containskey("exporttypeidentity") andalso "system.collections.generic.list(helloworld.mef.client.sms_supportedplatforms)".equals(exportdefinition.metadata.get_item("exporttypeidentity"))))', invalid exports may have been rejected.  resulting in: cannot set import 'helloworld.mef.neoservicemanagerimports.supportedplatforms (contractname="sms_supportplatformsexport")' on part 'helloworld.mef.neoservicemanagerimports'. element: helloworld.mef.neoservicemanagerimports.supportedplatforms (contractname="sms_supportplatformsexport") -->  helloworld.mef.neoservicemanagerimports  resulting in: exception occurred while trying create instance of type 'helloworld.mef.externalmainmenuexternalsubmenu'.  resulting in: cannot activate part 'helloworld.mef.externalmainmenuexternalsubmenu'. element: helloworld.mef.externalmainmenuexternalsubmenu -->  helloworld.mef.externalmainmenuexternalsubmenu -->  assemblycatalog (assembly="helloworld.mef, version=1.0.0.0, culture=neutral, publickeytoken=null")  resulting in: cannot export 'helloworld.mef.externalmainmenuexternalsubmenu (contractname="externalsubmenuforexternalmainmenucontract")' part 'helloworld.mef.externalmainmenuexternalsubmenu'. element: helloworld.mef.externalmainmenuexternalsubmenu (contractname="externalsubmenuforexternalmainmenucontract") -->  helloworld.mef.externalmainmenuexternalsubmenu -->  assemblycatalog (assembly="helloworld.mef, version=1.0.0.0, culture=neutral, publickeytoken=null") 

this code exception appears

var page = (from p in this.plugins                     e in p.entrypage                     e.metadata.navigateuri == this.targeturi.tostring()                     select e).single().createexport().value; 

residing in class (loading pages dynamically)

public class mefcontentloader : inavigationcontentloader     {         private pageresourcecontentloader pageresourcecontentloader = new pageresourcecontentloader();         private uri targeturi;          [importmany(allowrecomposition = true)]         public uiproviderbase[] plugins         {             get;             set;         }          public mefcontentloader()         {             compositioninitializer.satisfyimports(this);         }          #region inavigationcontentloader members          public iasyncresult beginload(uri targeturi, uri currenturi, asynccallback usercallback, object asyncstate)         {             this.targeturi = targeturi;             return pageresourcecontentloader.beginload(targeturi, currenturi, usercallback, asyncstate);         }          public bool canload(uri targeturi, uri currenturi)         {             // todo: handle             return true;         }          public void cancelload(iasyncresult asyncresult)         {             // todo: handle             pageresourcecontentloader.cancelload(asyncresult);         }          public loadresult endload(iasyncresult asyncresult)         {             if (this.plugins.length == 0 ||                 this.plugins.count(p => p.entrypage != null && p.entrypage.any(u => u.metadata.navigateuri == targeturi.tostring())) == 0)             {                 return pageresourcecontentloader.endload(asyncresult);             }              var page = (from p in this.plugins                     e in p.entrypage                     e.metadata.navigateuri == this.targeturi.tostring()                     select e).single().createexport().value;              return new loadresult(page);         }          #endregion       } 

to sum up: in assembly b (plugin) i'm exporting number of pages uiproviderbase in assembly b. pages need consume data assembly a(main silverlight app). assembly needs import uiproviderbase plugins, , them collection of menu pages adding them application. works type's defined in webservice, both assembly , b have reference to.

turns out, form of either type matching or type incompatability. in plugin (assembly b), added reference assembly , used type there:

[import(allowrecomposition = true)] public lazy<list<neoservicemanager.client.sms_supportedplatforms>> supportedplatforms {     get;     set; } 

i have not decorated export or import contractname, since it's not needed in case , type unambiguous.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -