wcf - Service 实现多个 ServiceContract 类型然后 Restful Service 抛出 ServiceActivationException

标签 wcf rest autofac

因此,出于某些考虑,我将最初的单一接口(interface)契约拆分为两个独立的契约。现在,我只有一个类(部分)实现了这两个接口(interface),我想通过 WCF 将它们全部用作 REST 服务。契约(Contract)如下所列。

我正在为 WCF 服务主机使用 Autofac。

下面是我的.svc文件代码

对于 WCF 服务:Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"

对于 RESTful 服务:Factory="Autofac.Integration.Wcf.AutofacWebServiceHostFactory, Autofac.Integration.Wcf"

WCF 示例:

对于wcf服务

public interface IEmployeeCommandService {}
public interface IEmployeeQueryService {}
public partial class EmployeeService : IEmployeeCommandService {}
public partial class EmployeeService : IEmployeeQueryService {}
public partial class EmployeeService {}

我的 Web.config 文件如下所示

<service behaviorConfiguration="ServiceBehavior" name="Application.EmployeeService">
    <endpoint address="" binding="basicHttpBinding" contract="Application.IEmployeeCommandService" />
    <endpoint address="" binding="basicHttpBinding" contract="Application.IEmployeeQueryService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>

我的EmployeeService.svc 代码看起来像

<%@ ServiceHost Service="Application.EmployeeService, Application"
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"%>

通过这种方法,普通 WCF 服务对我来说工作正常。浏览 EmployeeService.svc 时,我可以看到命令和查询服务操作。

我正在尝试如下所述的 RESTFul 服务的类似方法。但是当我浏览我的 RESTFul 服务时出现错误。

RESTFul 示例:

public interface IEmployeeRESTfulQueryService {}
public interface IEmployeeRESTfulCommandService {}
public partial class EmployeeRESTfulService {}
public partial class EmployeeRESTfulService : IEmployeeRESTfulCommandService {}
public partial class EmployeeRESTfulService : IEmployeeRESTfulQueryService {}

我的 web.confing 如下所示

  <service behaviorConfiguration="ServiceBehavior" name="Application.EmployeeRESTfulService">
    <endpoint name="Command" address="Command" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Application.IEmployeeRESTfulCommandService" />
    <endpoint name="Query" address="Query" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="Application.IEmployeeRESTfulQueryService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

<behavior name="webHttp">
 <webHttp helpEnabled="true" automaticFormatSelectionEnabled ="true" />
</behavior>

如果我浏览我的 .svc (RESTFul),我最终会遇到以下错误。

错误:

服务 'EmployeeRESTfulService' 实现了多种 ServiceContract 类型,并且配置文件中没有定义端点。 WebServiceHost 可以设置默认端点,但前提是服务仅实现单个 ServiceContract。要么将服务更改为仅实现单个 ServiceContract,要么在配置文件中明确定义服务的端点。

最佳答案

  1. 您是否在 .svc 文件上使用了 Factory=System.ServiceModel.Activation.WebServiceHostFactory?如果是这样,请删除 Factory 属性,因为您是在配置中定义端点。
  2. WCF 中的端点绑定(bind)到契约(Contract)(接口(interface));如果您有两个不同的接口(interface),则需要两个不同的端点。

更新以下编辑:restful 服务使用的FactoryAutofac.Integration.Wcf.AutofacWebServiceHostFactory、Autofac.Integration.Wcf,正在使用 WebServiceHost,它要求服务类只实现一个 [ServiceContract] 接口(interface)。您需要 Autofac 工厂吗?如果没有,只需删除该属性;否则,由于您已经通过配置定义了端点,我想它也应该与“普通”工厂 (AutofacServiceHostFactory) 一起使用(我从未使用过 Autofac,但您可以尝试一下)。

关于wcf - Service 实现多个 ServiceContract 类型然后 Restful Service 抛出 ServiceActivationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608329/

相关文章:

c# - 取代 BizTalk WCF 服务

通过 https 负载平衡器的 WCF http 服务

java - 如何管理休息服务中的 session ?

c# - OWIN + SignalR + Autofac

c# - 在不使用接口(interface)的情况下删除冗余的 C# 代码

.net - 具有大量数据的 WCF 服务的最佳实践?

java - 尝试使用 ZipInputStream 验证 com.sun.xml.internal.org.jvnet.mimepull.DataHead.ReadMultiStream

java - 保存后如何检索实体关系?

c# - 使用新的 .net-core-3 json 序列化程序进行依赖注入(inject)

c# - 使用 Autofac 的 Windows 服务保留引用