Search code examples
c#wcfjsonrest

WCF webHttpBinding error with method parameters. "At most one body parameter can be serialized without wrapper elements"


Operation '' of contract '' 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.

I am trying to expose a C# 4.0 WCF Service with JSON via the following config (set via the WCF Config Editor):

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="iPhoneAPI.API">
        <endpoint address="" behaviorConfiguration="NewBehavior0" binding="webHttpBinding"
          bindingConfiguration="" contract="iPhoneAPI.IApi" />
      </service>
    </services>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" bindingConfiguration="" />
    </protocolMapping>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NewBehavior0">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>

When I access /API.svc, I get the previously listed exception message.

If I only specify the following (parameter-less)contract, the service works:

[OperationContract]
[WebInvoke(
    Method = "GET", 
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "test")]
GenericApiResult<IEnumerable<LiveFeedEntity>> test();

If I have methods that require parameters that are non-strings, I get the previously listed exception.

Example:

[OperationContract]
[WebInvoke(
    Method = "GET", 
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "login")]
LoginApiResult Login(String UserName, String Password);

If I change this function like so:

[OperationContract]
[WebInvoke(
    Method = "GET", 
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "login/{UserName}/{Password}")]
LoginApiResult Login(String UserName, String Password);

It works; but this is only possible for parameters of Type String. How do I respolve this for my other functions like:

[OperationContract]
[WebInvoke(
    Method = "POST", 
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json, 
    UriTemplate = "logout")]
GenericApiResult<bool> Logout(Guid SessionKey);

Tried a lot of google searches, but turned up empty handed, any help is appreciated.

Cheers,

Nick.


Solution

  • Have you tried setting the WebInvokeAttribute.BodyStyle to Wrapped as the error recommends?