2010-03-23 63 views
2

我在服務的方法簽名(例如int,string)中只有簡單的數據類型。我的服務類實現了單一的ServiceContract接口,比如說IMathService,而這個接口又繼承了一些其他的基本接口,比如說IAdderService。我想在單個端點上使用接口協定IAdderService作爲服務公開MathService。然而,一些瞭解IMathService的clinet應該能夠訪問IMathService在該單個端點上提供的額外服務,即只需將IAdderService類型化爲IMathService即可。如何在單個端點上公開WCF服務中具有多繼承的服務契約接口

//Interfaces and classes at server side 
[ServiceContract] 
public interface IAdderService 
{ 
[OperationContract] 
int Add(int num1, int num2); 
} 

[ServiceContract] 
public interface IMathService : IAdderService 
{ 
[OperationContract] 
int Substract(int num1, int num2); 
} 

public class MathService : IMathService 
{ 
#region IMathService Members 
public int Substract(int num1, int num2) 
{ 
    return num1 - num2; 
} 
#endregion 

#region IAdderService Members 
public int Add(int num1, int num2) 
{ 
    return num1 + num2; 
} 
#endregion 
} 

//Run WCF service as a singleton instace 
MathService mathService = new MathService(); 
ServiceHost host = new ServiceHost(mathService); 
host.Open(); 

服務器端配置:

<configuration> 
    <system.serviceModel> 
    <services> 
      <service name="IAdderService" 
     behaviorConfiguration="AdderServiceServiceBehavior"> 
     <endpoint address="net.pipe://localhost/AdderService" 
     binding="netNamedPipeBinding" 
     bindingConfiguration="Binding1" 
     contract="TestApp.IAdderService" /> 
     <endpoint address="mex" 
     binding="mexNamedPipeBinding" 
     contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.pipe://localhost/AdderService"/> 
      </baseAddresses> 
     </host> 
      </service> 
      </services> 
     <bindings> 
     <netNamedPipeBinding> 
     <binding name="Binding1" > 
      <security mode = "None"> 
      </security> 
      </binding > 
     </netNamedPipeBinding> 
     </bindings> 

     <behaviors> 
     <serviceBehaviors> 
    <behavior name="AdderServiceServiceBehavior"> 
      <serviceMetadata /> 
     <serviceDebug includeExceptionDetailInFaults="True" /> 
    </behavior> 
     </serviceBehaviors> 
     </behaviors> 
    </system.serviceModel> 
    </configuration> 

客戶端imeplementation:

IAdderService adderService = new 
ChannelFactory<IAdderService>("AdderService").CreateChannel(); 
int result = adderService.Add(10, 11); 

IMathService mathService = adderService as IMathService; 
result = mathService.Substract(100, 9); 

客戶端配置:

<configuration> 
     <system.serviceModel> 
      <client> 
      <endpoint name="AdderService" address="net.pipe://localhost/AdderService" binding="netNamedPipeBinding" bindingConfiguration="Binding1" contract="TestApp.IAdderService" /> 
      </client> 

      <bindings> 
      <netNamedPipeBinding> 
     <binding name="Binding1" maxBufferSize="65536" maxConnections="10"> 
      <security mode = "None"> 
      </security> 
     </binding > 
      </netNamedPipeBinding> 
      </bindings> 
     </system.serviceModel> 
     </configuration> 

使用上面的代碼和配置,我不能類型轉換IAdd erService instnace到IMathService,它失敗,我在客戶端獲得IMathService的空實例。

我的觀察是,如果服務器向客戶端公開IMathService,則客戶端可以安全地向IAdderService進行類型轉換,反之亦然。但是,如果服務器公開IAdderService,則該類型轉換失敗。

有沒有解決這個問題的方法?或者我是以一種錯誤的方式去做的。

回答

0

正如您所觀察到的,您必須公開派生服務(MathService)而不是AdderService。

WCF不知道AdderService碰巧實際上是實現MathService接口的MathService。它被視爲AdderService - 因此無法將其轉換爲派生類。

0

我完全複製了你的情況,除了我使用了IMathService端點而不是IAdderService端點。

IAdderService adderService = new ChannelFactory<IMathService>("MathService").CreateChannel(); 
int result = adderService.Add(10, 11); 

IMathService mathService = adderService as IMathService; 
result = mathService.Substract(100, 9); 

這適用於我。您不能將基類型轉換爲派生類型。但是,你可以做相反的事情。