多个WCF服务实现相同的服务合同接口多个、接口、合同、WCF

2023-09-03 17:25:41 作者:鹤川.

是否有可能为多个WCF服务,以实现相同的服务协定接口?

我想要做的就是让一个测试服务是可以互换的真正的服务,并指定服务的配置文件中使用。

例如:

  [的ServiceContract]
公共接口IUselessService
{
  [OperationContract的]
  字符串的GetData(int值);
}
 

测试实施

 公共类TestService的:IUselessService
{
  公共字符串的GetData(int值)
  {
    返回这是测试;
  }
}
 
基于HTTP协议的API接口测试

皇家班

 公共类RealService:IUselessService
{
  公共字符串的GetData(int值)
  {
    返回的String.Format(您输入:{0},值);
  }
}
 

解决方案

谢谢你们为您解答。我有一个解决方案,现在对我的作品,没有把界面在一个单独的组件,并在GAC。我不看使用界面等项目,只需使用同一个接口的多个服务于同一个项目。

我试图做的是使WCF服务的配置文件中RealService和TestService的之间的变化,所以客户端就不会知道其中的差别(客户端就不必更改其配置,使其指向一个不同。 SVC文件)。我不知道这是可能的,或者至少如果是,这绝不是straightfoward。

我在做什么,现在只是WCF服务的配置文件中同时指定服务,然后我点了客户一个或另一个基于我想要的服务。由于这WCF服务仅供内部使用,我们有客户端和服务的控制权,这是个不错的折衷。该解决方案可能是其意图反正更加明确。

下面是配置文件的片段:

 <服务>
      <服务behaviorConfiguration =WcfService1.Service1Behavior
               NAME =WcfService1.TestService>
        <端点地址=绑定=basicHttpBinding的bindingConfiguration =testBasicHttpBinding
          合同=WcfService1.IUselessService>
        < /端点>
        <端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
      < /服务>
      <服务behaviorConfiguration =WcfService1.Service1Behavior
               NAME =WcfService1.RealService>
        <端点地址=绑定=basicHttpBinding的bindingConfiguration =testBasicHttpBinding
         合同=WcfService1.IUselessService>
        < /端点>
        <端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
      < /服务>
    < /服务>
 

Is it possible for multiple wcf services to implement the same service contract interface?

What I want to do is allow for a test service to be interchangeable for the real service, and to specify which service to be used in the configuration file.

For example:

[ServiceContract]
public interface IUselessService  
{  
  [OperationContract]   
  string GetData(int value);   
}  

Test implementation

public class TestService : IUselessService  
{  
  public string GetData(int value)  
  {  
    return "This is a test";   
  }  
}  

Real class

public class RealService : IUselessService  
{  
  public string GetData(int value)  
  {  
    return string.Format("You entered: {0}", value);  
  }  
}

解决方案

Thanks guys for your answers. I have a solution now that works for me, without putting the interface in a separate assembly and in the GAC. I'm not looking at using the interface for other projects, just using the same interface for multiple services in the same project.

What I was trying to do was make the change between RealService and TestService in the configuration file of the WCF service, so the client would not know the difference (the client would not have to change its configuration to point to a different .svc file). I'm not sure this is possible, or it least if it is, it is definitely not straightfoward.

What I am doing now is just specifying both services in the configuration file of the WCF service, and then I point the client to one or the other based on which service I want. Since this WCF service is for internal use only and we have control of both the client and the service, it is not a bad tradeoff. This solution is probably more explicit in its intentions anyways.

Here is the snippet of the configuration file:

<services>
      <service behaviorConfiguration="WcfService1.Service1Behavior"
               name="WcfService1.TestService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="testBasicHttpBinding" 
          contract="WcfService1.IUselessService">              
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
      <service behaviorConfiguration="WcfService1.Service1Behavior"
               name="WcfService1.RealService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="testBasicHttpBinding"
         contract="WcfService1.IUselessService">             
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>