c# - 使 WCF .NET 服务实现实现多个 ServiceContract 接口(interface)

标签 c# .net wcf rest exception

我正在使用 WCF 开发 RESTful Web 服务。我希望有一个我的所有服务都实现的 ServiceContract 接口(interface),但是,我还希望每个服务另外实现自己的方法。

在我的 Global.asax 文件中,我初始化了服务路由:

RouteTable.Routes.Add(new ServiceRoute("iOSAppService", new WebServiceHostFactory(), typeof(Service.iOSAppService)));
RouteTable.Routes.Add(new ServiceRoute("AndroidAppService", new WebServiceHostFactory(), typeof(Service.AndroidAppService)));
RouteTable.Routes.Add(new ServiceRoute("WindowsPhoneAppService", new WebServiceHostFactory(), typeof(Service.WindowsPhoneAppService)));

每个服务都必须实现IAppService接口(interface):

[ServiceContract]
public interface IAppService

其实现如下:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class iOSAppService : IAppService

但是,例如,我也希望使用 iOSAppService 来实现 IiOSApService 接口(interface):

[ServiceContract]
public interface IiOSAppService

从而实现:

public class iOSAppService : IAppService, IiOSAppService

但是,这会导致以下异常:

Service 'iOSAppService' implements multiple ServiceContract types, and no endpoints are defined in the configuration file. WebServiceHost can set up default endpoints, but only if the service implements only a single ServiceContract. Either change the service to only implement a single ServiceContract, or else define endpoints for the service explicitly in the configuration file.

有谁知道我如何实现我的目标?

最佳答案

使您的特定界面如下所示:

[ServiceContract]
public interface IiOSAppService : IAppService

然后

public class iOSAppService : IiOSAppService

编辑:

在服务方面,请确保您拥有:

<system.serviceModel>
  <services>
    <service name="YourNamespace.iOSAppService">
      <endpoint binding="webHttpBinding" contract="YourNamespace.IiOSAppService" behaviorConfiguration="web">
      </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>

关于c# - 使 WCF .NET 服务实现实现多个 ServiceContract 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11649640/

相关文章:

.net - 如何测试 '-'是否存在,但字符 '['和 ']'之间不存在?

c# - 无法调用服务,WCF 测试客户端?

c# - 'circular linked list builder' 的最快算法

c# - 使用自定义中间件 Asp.Net-Core 添加响应 header

c# - CRM 2013 通过自定义工作流程获取 CRM URL

C#、MAF、单独 AppDomain 中的未处理异常管理

c# - 如何仅向注册用户显示某些菜单链接

c# - WCF 服务引用中的接口(interface)参数/返回类型

c# - 如何使用 wcf 反序列化不同的接口(interface)实现

wcf - NetTcpBinding.MaxConnections 是否限制到端点的并发连接数或......?