2010-03-03 98 views

回答

0

我想你可以做到這一點,有以下幾個步驟:

1)建立一個共同的類庫與合同(接口)

[ServiceContract] 
public interface ISimpleService 
{ 
} 

2)WCF服務創建另一個類庫實現,並用MEF的導出屬性標記你的WCF服務類。

[Export(typeof(ISimpleService))] 
public class SimpleService: ISimpleService 
{ 
} 

3)然後,在主機應用程序(ASP.NET MVC在這種情況下)創建基於組件目錄例如是MEF組合物的容器。

protected void Application_Start() 
{ 
    var catalog = new AssemblyCatalog(...); 
    var container = new CompositionContainer(catalog); 
    container.ComposeParts(this); 
} 

4)然後,你將能夠與MEF

[Import] 
public ISimpleService SimpleService { get; set; } 

5)然後,您可以創建WCF服務主機爲服務實現

var serviceHost = new ServiceHost(SimpleService, ...); 

希望導入您的服務實現這有助於。