2

我在服务的方法签名中只有简单的数据类型(例如 int、string)。我的服务类实现了单个 ServiceContract 接口,比如 IMathService,而这个接口又继承自其他一些基础接口,比如 IAdderService。我想使用接口合同 IAdderService 将 MathService 公开为单个端点上的服务。然而,一些知道 IMathService 的客户应该能够访问 IMathService 在该单个端点上提供的额外服务,即只需将 IAdderService 类型转换为 IMathService。

//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>

客户端实现:

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>

使用上面的代码和配置,我无法将 IAdderService 实例类型转换为 IMathService,它失败了,并且我在客户端获得了 IMathService 的空实例。

我的观察是,如果服务器将 IMathService 暴露给客户端,那么客户端可以安全地类型转换为 IAdderService,反之亦然。但是,如果服务器公开 IAdderService,则类型转换失败。

有什么解决办法吗?还是我做错了。

4

2 回答 2

0

正如您所观察到的,您必须公开派生服务 (MathService) 而不是 AdderService。

WCF 不知道 AdderService 实际上是实现 MathService 接口的 MathService。它被视为 AdderService - 因此无法将其转换为派生类。

于 2010-03-23T19:09:09.450 回答
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);

这对我有用。您不能将基类型强制转换为派生类型。但是,您可以做相反的事情。

于 2010-06-16T20:02:14.850 回答