2016-02-14 69 views
0

此代碼工作在普通類:爲什麼WCF類需要接口繼承,但普通類不是?

public interface IServiceBase 
{ 
    void BaseMethod(); 
} 

public class ServiceBase : IServiceBase 
{ 
    public void BaseMethod() { } 
} 

public interface ITestService // : IServiceBase - it doesn't reqiure 
           // in plain classes but in WCF it does. 
{ 
    void TestMethod(); 
} 

public class TestService : ServiceBase, ITestService 
{ 
    public void TestMethod() { } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var ts = new TestService(); 
     ts.BaseMethod(); 
    } 
} 

但在WCF上下文中的客戶端無法看到BaseMethod直到你從IServiceBase inherite ITestService:

public interface ITestService : IServiceBase 

爲什麼?

我想

TestService : ServiceBase 

已經包括

TestService : IServiceBase 

在普通班是這樣,但在WCF事實並非如此。

我的問題是不是這個問題Interface inheritance and derived classes

+0

你如何創建你的代理類?通過代碼生成或通過使用ServiceChannel在客戶端和服務器之間手動共享相同的接口? – CodeNotFound

+0

通過Visual Studio中的AddServiceReference選項。 – Alex34758

+1

然後@TomTom的答案是正確的。 – CodeNotFound

回答

3

由於WCF是基於自動執行(在客戶端)代理和確定合同(接口),一個服務公開 - 而實現可能,通常是在一個不同的項目。使用正確工具(界面)作業的基本設計原則(描述界面)。

+0

所以,如果客戶端代理可以以某種方式看到實現(TestService),他們可以發現IServiceBase已經通過從ServiceBase繼承而在TestService中實現了,對吧?但是通過設計.NET創建者決定不在WCF服務中執行它,而是在普通類中執行它?對? – Alex34758

+1

他們沒有看到實施。將數據包中的請求打包。 – TomTom

相關問題