我正在使用 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/