简体   繁体   English

WCF服务中的WebScriptEnablingBehavior不适用于POST方法

[英]WebScriptEnablingBehavior in WCF service doesn't work with POST method

I would like to take advantage of WebScriptEnablingBehavior in my WCF REST service to provide the ability to generate a javascript client proxy (and the strongly typed objects that go with it). 我想在WCF REST服务中利用WebScriptEnablingBehavior的功能来生成javascript客户端代理(以及随之而来的强类型对象)。
It works fine with the GET and client.DownloadData() method (parameters passed as query strings) but I'm struggling to make it work with POST and client.UploadData(...) . 它可以与GETclient.DownloadData()方法(作为查询字符串传递的参数)配合使用,但是我正努力使其与POSTclient.UploadData(...)

The problem is that the parameters end-up both null in my service method (no error/exception, I can debug through it just fine... They're just null). 问题在于参数最终在我的服务方法中都为空(没有错误/异常,我可以通过它进行调试就好了……它们只是空)。

The web.config file web.config文件

<services>
  <service behaviorConfiguration="ServiceBehavior" name="ServWS_Main_2.WS_Main_2">
    <endpoint address="" behaviorConfiguration="JSONOnly" binding="webHttpBinding"
      bindingConfiguration="StreamedRequestWebBinding" contract="ServWS_Main_2.IServWS" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="JSONOnly">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>

The service contract 服务合同

[ServiceContract]
public interface IServWS
{
    [OperationContract]
    [WebInvoke(Method = "POST")]
    ProviderInfo AddTo(string serviceName, BubbleInfo bubble);
}

The implementation 实施

public class WS_Main_2 : IServWS
{
    public ProviderInfo AddTo(string serviceName, BubbleInfo bubble)
    {
        // at this point both serviceName and bubble  are null
    }
}

Definition of the complex type passed as parameter 作为参数传递的复杂类型的定义

[DataContract]
public class BubbleInfo
{

    [DataMember(EmitDefaultValue = false)]
    public string Text { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string Caption { get; set; }
}

The call from the client 客户打来的电话

BubbleInfo bub = new BubbleInfo() { Text = "test2" };

MemoryStream stream = new MemoryStream();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BubbleInfo));
serializer.WriteObject(stream, bub);

var url = GetURL() + "/addto?serviceName=MyService";
byte[] data = client.UploadData(url, "POST", stream.ToArray());

Note that it works fine if I don't use WebScriptEnablingBehavior but webHttp and URITemplate instead. 请注意,如果我不使用WebScriptEnablingBehavior而是使用webHttpURITemplate ,则可以正常工作。
Tested with Visual Studio built-in web server as well as IIS Express 7.5. 已通过Visual Studio内置Web服务器以及IIS Express 7.5进行了测试。

Thanks. 谢谢。

I think this is related to the "body style" of the request message. 我认为这与请求消息的“主体样式”有关。 You may be sending a 'bare' JSON message to the service when when the service actually expects a 'wrapped' message (with an extra wrapper around the JSON that is generally the parameter name.) 当服务实际期望“包装”消息时,您可能正在向该服务发送“裸” JSON消息(带有附加的JSON包装,通常是参数名称)。

What you need to pay attention to is the WebInvokeAttribute.BodyStyle property. 您需要注意的是WebInvokeAttribute.BodyStyle属性。 It gets and sets the body style of the messages that are sent to and from the service operation. 它获取并设置发送到服务操作和从服务操作发送的消息的正文样式。 It ocntrols whether to wrap requests and responses sent to and received from the service operation the attribute is applied to. 它控制是否包装发送给属性应用到的服务操作的请求和响应。 Generally the returned JSON is wrapped by JavaScript function which can be executed cross-domain, so you may need to remove that by switching to a 'bare' body style. 通常,返回的JSON被可以跨域执行的JavaScript函数包装,因此您可能需要通过切换为“裸露”样式来删除它。

What happens if you set the BodyStyle property on WebInvokeAttribute explicitly to Bare or to Wrapped, and try again? 如果将WebInvokeAttribute的BodyStyle属性显式设置为Bare或Wrapped,然后重试,会发生什么情况? Does that fix the problem for you? 这样可以为您解决问题吗? I think it should! 我认为应该!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM