【问题标题】:Enable SOAP And REST On Same WCF Service And Contract在同一 WCF 服务和合同上启用 SOAP 和 REST
【发布时间】:2020-12-15 02:02:05
【问题描述】:

我有一个带有 basichttpbinding 的现有 wcf SOAP 服务。现在我想将其扩展为仅包含其余属性的合同,以便现有方法不会影响使用此合同的客户。

从服务中粘贴一些主要代码sn-ps(不包括不必要的代码),如果您需要什么,请告诉我:

 public interface IMessages
 {
    // existing contract
    [OperationContract(Name = "LoadMessage", IsOneWay = true)]
    void LoadMessage(Guid categoryId, int fileId);

    // new REST contract
    [WebInvoke(Method = "POST",
        UriTemplate = "/LoadMessagesApi/{param}",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    [Description("Inbound Message")]
    void LoadMessagesApi(string param);
}

公共接口 IPayment: IMessages { }

配置:

<service name="Services.PaymentService">

    <endpoint address="xmlservice" 
              binding="webHttpBinding"
              behaviorConfiguration="RestBehavior"
              contract="Services.Interfaces.IPayment""/>
    <endpoint address="" binding="wsHttpBinding"
              bindingConfiguration="wsHttpBindingConfig" 
              name="httpGateway" 
              contract="Services.Interfaces.IPayment" />
  </service>
    

<behaviors>
  <serviceBehaviors>
    <behavior name="RestBehavior">
      <!--Behaviour for REST endpoint for HELP enability-->
      <webHttp helpEnabled ="true"></webHttp>
    </behavior>
  </endpointBehaviors>
</behaviors>

但我收到此错误:

合约“IMessages”的“LoadMessage”操作指定了多个要序列化的请求主体参数,而无需任何包装器元素。最多一个 body 参数可以在没有包装元素的情况下被序列化。删除额外的正文参数或将 WebGetAttribute/WebInvokeAttribute 上的 BodyStyle 属性设置为 Wrapped。

为什么我在第一个没有添加 webget 或 webinvoke 属性的合同上出现 bodystyle 错误?有人可以指点吗?

【问题讨论】:

  • 您的服务接口有问题。在ServiceContract中,如果其中一个方法使用WebInvoke,其他方法需要使用WebInvoke或webget,所以解决方法是在LoadMessage中添加WebInvoke或webget,或者将LoadMessagesApi上面的WebInvoke修改为OperationContract。
  • 好的,解决了这个问题。您可以添加答案,我将其标记为答案,谢谢

标签: wcf wcf-binding wcf-rest


【解决方案1】:

您的服务接口有问题。在ServiceContract中,如果其中一个方法使用WebInvoke,其他方法需要使用WebInvoke或webget,所以解决方法是在LoadMessage中添加WebInvoke或webget,或者将LoadMessagesApi上面的WebInvoke修改为OperationContract。

【讨论】:

    猜你喜欢
    • 2020-12-06
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2019-08-15
    相关资源
    最近更新 更多