2012-07-27 60 views
0

是否可以使用MEF來組成WCF RIA域服務?MEF - 如何撰寫WCF RIA DomainService?

比方說,我有以下域服務屬性:

[Import(RequiredCreationPolicy = CreationPolicy.Shared)] 
public TestClass Dependency{ get; set; } 

而且TestClass中..

[Export] 
public class TestClass 
{ 
    public void Do() 
    { 

    } 
} 

而且我有一個DomainServiceFactory類:

public class DomainServiceFactory : IDomainServiceFactory 
{ 
    private readonly ComposablePartCatalog _catalog; 
    private readonly CompositionContainer _container; 

    public DomainServiceFactory() 
    { 
     _catalog = new TypeCatalog(typeof(DomainService1), typeof(DomainService2)); 

     _container = new CompositionContainer(_catalog); 
     //What should I call here? _container.SatisfyImportsOnce(this); 
    } 

    #region Implementation of IDomainServiceFactory 

    public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context) 
    { 
     DomainService domainService; 
     var export = _container.GetExports(domainServiceType, null, null).FirstOrDefault(); 
     if(export != null) 
     { 
      domainService = (DomainService) export.Value; 
     } 
     else 
     { 
      domainService = (DomainService)Activator.CreateInstance(domainServiceType); 
     } 


     domainService.Initialize(context); 

     return domainService; 

    } 

    public void ReleaseDomainService(DomainService domainService) 
    { 
     domainService.Dispose(); 
    } 

    #endregion 
} 

是否SatisfyImportsOnce構建容器的方法?輸入參數是一個ComposablePart,我應該傳入什麼?

問候。

回答

0

你已經構建你的容器,當你撥打:

_container = new CompositionContainer(_catalog); 

而當你沒有得到你的DomainServiceFactory很少有一點調用_container.SatisfyImportsOnce(this);

當你調用export.Value,任何進口將自動滿足您的域服務類型的任何導入。你錯過了什麼/想要達到什麼目的?

+0

當調用GetExports(...)時,我得到空。這就是爲什麼我認爲它沒有初始化。 – Alberto 2012-07-27 17:34:14

+0

您是否已經使用特定合約導出了您的類型I.e [Export(typeof(DomainService))]還是僅[Export]? – 2012-07-27 17:49:09

+0

只需[導出]。星期一將嘗試[導出(typeof(DomainService))]。沒有意識到。 – Alberto 2012-07-27 17:52:06