wcf - Enable SOAP And REST On Same WCF Service And Contract
问题描述
I have an existing wcf SOAP service with basichttpbinding. Now I want to extend it to have one more contract with only rest attributes so that existing method doesn't impact the clients using this contract.
Pasting some major code snippets from the service (excluding unnecessary code), if you need anything, please let me know:
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);
}
public interface IPayment: IMessages { }
Config:
<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>
But I get this error:
Operation 'LoadMessage' of contract 'IMessages' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.
Why am I getting bodystyle error on the first contract where I haven't added webget or webinvoke attribute? Can someone point please?
解决方案
There is a problem with your service interface. In a ServiceContract, if one of the methods uses WebInvoke, other methods need to use WebInvoke or webget, so the solution is to add WebInvoke or webget to LoadMessage, or modify the WebInvoke above LoadMessagesApi to OperationContract.
推荐阅读
- javascript - 将字符串转换为数字,具体取决于它们是否具有后缀,例如 1.5K
- javascript - 无法使用 VueRouter 浏览我的不同路由器链接
- python - 将 Python 列表转换为单个字符串
- javascript - 即使在相同的表单/div中,复选框也不会检查和隐藏
- python - 赋值前引用的函数错误变量
- c# - 不允许输出到总线系统 CAN 通道 X
- python - 有什么方法可以对字典中的所有列表值求和吗?
- java - 如何在 Hibernate 查询语言中查询具有关联实体的所有实体?
- java - 无法从java中的布尔方法获得返回
- .net - WPF .net5 Windows Server 错误:找不到 Microsoft.WindowsDesktop.App 框架