连接到RESTful服务的WCF客户端无法正常工作

时间:2012-10-11 04:52:16

标签: wcf

我正在尝试使用svcutil生成的客户端与RESTful WCF服务进行通信。

服务合同定义为:

public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "/GetTest?a={a}&b={b}&c={c}")]
    int GetTest(int a, int b, int c);
}

我使用Visual Studio引用此服务,并使用生成的客户端代码调用GetTest操作。不幸的是,我收到了这条消息:

Operation 'GetTest' of contract 'IService1' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapperelements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped

但是当我从网络浏览器请求相应的网址时,它显示了正确的返回值。

这很奇怪。生成的客户端代码有什么问题吗?或者我错误配置了什么?

以下是我的客户端配置:

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="Service1EndPointBehavior">
                <webHttp />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <client>
        <endpoint address="http://localhost:8010/Service1/" behaviorConfiguration="Service1EndPointBehavior"
            binding="webHttpBinding" contract="ServiceReference1.IService1"
            name="Service1EndPoint" />
    </client>
</system.serviceModel>

感谢。

1 个答案:

答案 0 :(得分:0)

将此属性添加到您的方法

[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)]

说明:

http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webmessagebodystyle.aspx