我想利用WebScriptEnablingBehavior
我的 WCF REST 服务来提供生成 javascript 客户端代理(以及随之而来的强类型对象)的能力。
它适用于GET
andclient.DownloadData()
方法(作为查询字符串传递的参数),但我正在努力让它与POST
and一起使用client.UploadData(...)
。
问题是我的服务方法中的参数最终都为空(没有错误/异常,我可以通过它进行调试......它们只是空的)。
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>
服务合同
[ServiceContract]
public interface IServWS
{
[OperationContract]
[WebInvoke(Method = "POST")]
ProviderInfo AddTo(string serviceName, BubbleInfo bubble);
}
实施
public class WS_Main_2 : IServWS
{
public ProviderInfo AddTo(string serviceName, BubbleInfo bubble)
{
// at this point both serviceName and bubble are null
}
}
作为参数传递的复杂类型的定义
[DataContract]
public class BubbleInfo
{
[DataMember(EmitDefaultValue = false)]
public string Text { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Caption { get; set; }
}
来自客户的电话
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());
请注意,如果我不使用WebScriptEnablingBehavior
butwebHttp
而URITemplate
代替,它可以正常工作。
使用 Visual Studio 内置 Web 服务器以及 IIS Express 7.5 进行测试。
谢谢。