协定“”得操作“”指定了多个要序列化得请求正文参数,而不使用任何 Package 元素.最多只能序列化一个正文参数,而不使用 Package 元素.请删除多余得正文参数,或将WebGetAttribute/WebInvokeAttribute得BodyStyle属性设置为Wrapped.
我正在尝试通过以下配置(通过WCF配置编辑器设置)公开使用JSON的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>
字符串
当我访问/API.svc时,我得到了前面列出的异常消息。
如果我只指定以下(无参数)协定,则服务可以工作:
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "test")]
GenericApiResult<IEnumerable<LiveFeedEntity>> test();
型
如果我的方法需要非字符串的参数,我会得到前面列出的异常。
范例:
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "login")]
LoginApiResult Login(String UserName, String Password);
型
如果我这样修改这个函数:
[OperationContract]
[WebInvoke(
Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "login/{UserName}/{Password}")]
LoginApiResult Login(String UserName, String Password);
型
它是有效的,但这只适用于String类型的参数。我如何为我的其他函数响应这个问题,比如:
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "logout")]
GenericApiResult<bool> Logout(Guid SessionKey);
型
尝试了很多谷歌搜索,但一无所获,任何帮助都是感激不尽的。
干杯,
尼克
3条答案
按热度按时间c8ib6hqw1#
你有没有试过将WebInvokeAttribute.BodyStyle设置为错误建议的Wrapped?
5jvtdoz22#
问题是你的UriTemplate必须指定所有传入的值,除了一个。使用字符串以外的复杂类型作为参数是可以的,我们经常发送轻量级的JSON对象到我们的服务,它们也很好。这里是一个使用你最后一个例子的例子。
字符串
会起作用,因为只有一个参数被传入,并且它应该在帖子正文中,因为它不是作为URL参数传入的,但是你只能传入一个body参数(在帖子正文中传递的参数,而不是URL)。
你可以像这样改变你的第一个方法
型
它会工作,但密码会在帖子正文中。在这个特定的例子中,最好的方法是像第二个例子那样做,
型
你说得通吗?基本上所有传入的值都需要表示为URL参数,除了可以在post body中传递的值。如果您需要在post body中传递多个值,请创建一个具有所需多个值的轻量级对象,并在post body中接受整个对象。
x4shl7ld3#
WCF不支持多个参数,如果你需要在一个post方法操作中传递多个参数,那么我们需要将
BodyStyle
设置为Wrapped
。因此,在您的情况下,您必须将您的操作合同更改为以下内容:
字符串
来源:Click Here