WCF webHttpBinding错误与方法参数。“最多可以在没有包装器元素的情况下序列化一个主体参数”

时间:2021-07-17 20:28:45

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.

“契约”操作指定多个请求主体参数,不需要任何包装器元素就可以序列化。最多可以不使用包装器元素序列化一个主体参数。要么删除额外的body参数,要么将WebGetAttribute/WebInvokeAttribute上的BodyStyle属性设置为wrap。

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

我尝试通过以下配置(通过WCF配置编辑器设置)来公开一个c# 4.0 WCF服务:

<?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.

当我访问/ API。svc,我得到前面列出的异常消息。

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.

尼克。

3 个解决方案

#1


33  

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

您试过设置WebInvokeAttribute了吗?按错误建议的包体样式?

#2


22  

The problem is that your UriTemplate must specify all the values being passed in except for one. It is fine to have complex types other than strings as parameters, we frequently send lightweight json objects to our services and they come in just fine. Here is an example using your last example.

问题是,您的UriTemplate必须指定除一个以外的所有值。使用复杂类型而不是字符串作为参数是可以的,我们经常将轻量级的json对象发送到我们的服务中,它们非常好。下面是使用最后一个例子的例子。

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

Would work because there is only one parameter being passed in, and it is expected to be in the post body because it is not passed in as a URL parameter, but you can only pass in one body parameter (a parameter passed in the body of the post, not the URL).

会工作,因为只有一个参数传递,它预计将在邮局的身体,因为它不是在URL作为一个参数传递,但是你身体只能传递一个参数(参数传入的主体,而不是URL)。

You could change your first method like this

你可以像这样改变你的第一个方法

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

and it would work, but Password would come in the post body. The best way in this particular example is to do what you did for the second example like so

它可以工作,但是密码会进入邮件正文。在这个特定的例子中,最好的方法是像这样做第二个例子

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

Does that make sense? Basically all values passed in need to be represented as a URL parameter, except for one that can be passed in the post body. If you need multiple values passed in the post body, make a lightweight object that has the multiple values you need and accept that whole object in the post body.

这说得通吗?基本上所有传入的值都需要表示为URL参数,除了可以在post主体中传递的值。如果需要在post主体中传递多个值,那么创建一个具有所需多个值的轻量级对象,并接受post主体中的整个对象。

#3


6  

WCF doesn't support more than one parameter with bare body, if you need pass several parameters in one post method operation, then we need set the BodyStyle to Wrapped.

WCF不支持多个参数,如果您需要在一个post方法操作中传递几个参数,那么我们需要将BodyStyle设置为包。

So in your case you'd have to change your operation contract to the following:

因此,在你的情况下,你必须将你的经营合同改为:

[WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);

Source: Click Here

来源:点击这里

#1


33  

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

您试过设置WebInvokeAttribute了吗?按错误建议的包体样式?

#2


22  

The problem is that your UriTemplate must specify all the values being passed in except for one. It is fine to have complex types other than strings as parameters, we frequently send lightweight json objects to our services and they come in just fine. Here is an example using your last example.

问题是,您的UriTemplate必须指定除一个以外的所有值。使用复杂类型而不是字符串作为参数是可以的,我们经常将轻量级的json对象发送到我们的服务中,它们非常好。下面是使用最后一个例子的例子。

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

Would work because there is only one parameter being passed in, and it is expected to be in the post body because it is not passed in as a URL parameter, but you can only pass in one body parameter (a parameter passed in the body of the post, not the URL).

会工作,因为只有一个参数传递,它预计将在邮局的身体,因为它不是在URL作为一个参数传递,但是你身体只能传递一个参数(参数传入的主体,而不是URL)。

You could change your first method like this

你可以像这样改变你的第一个方法

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

and it would work, but Password would come in the post body. The best way in this particular example is to do what you did for the second example like so

它可以工作,但是密码会进入邮件正文。在这个特定的例子中,最好的方法是像这样做第二个例子

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

Does that make sense? Basically all values passed in need to be represented as a URL parameter, except for one that can be passed in the post body. If you need multiple values passed in the post body, make a lightweight object that has the multiple values you need and accept that whole object in the post body.

这说得通吗?基本上所有传入的值都需要表示为URL参数,除了可以在post主体中传递的值。如果需要在post主体中传递多个值,那么创建一个具有所需多个值的轻量级对象,并接受post主体中的整个对象。

#3


6  

WCF doesn't support more than one parameter with bare body, if you need pass several parameters in one post method operation, then we need set the BodyStyle to Wrapped.

WCF不支持多个参数,如果您需要在一个post方法操作中传递几个参数,那么我们需要将BodyStyle设置为包。

So in your case you'd have to change your operation contract to the following:

因此,在你的情况下,你必须将你的经营合同改为:

[WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);

Source: Click Here

来源:点击这里