wcf 接收post数据_WCF使用小结:(1)WCF接收HTTP POST数据的处理方法

在WCF 4.0中,为我们创建Restful API有了更好的支持。通过定义UriTemplate,WebInvoke就可以快速开发API接口。

这里我记录一下HTTP POST数据时要如何接收POST过来的数据。

1,方法一:Stream inputStream 输入流方法(注意看方法

例如我的代码

[OperationContract]

[WebInvoke(Method= "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Bare,

UriTemplate= "json/ui/{projectName}/{entityName}?q={queryString}&id={indentity}")]

UpdateOrInsertEntityResponse UpdateOrInsertEntityResponse (String projectName, String entityName, String queryString,String indentity,Stream pstream);

UriTemplate定义了参数匹配关系。

"json/ui/{projectName}/{entityName}?q={queryString}&id={indentity}"

对应的参数

String projectName, String entityName, String queryString,String indentity

名称必须相同,否则不能匹配。所有字段必须是String类型。

如何获取POST过来的数据信息。

定义Stream pstream参数。

如果你现在运行应用程序的话,会在页面爆出一个错误信息:

System.InvalidOperationException: For request inoperation UpdateOrInsertEntityResponse to be a stream the operation

must have a single parameter whose typeis Stream.

如何解决。

第一步,修改你自己的Service.svc文件。

将原始的

CodeBehind="EntityService.svc.cs" %>

增加新的属性:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

最后结果是:

CodeBehind="EntityService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

修改配置文件Web.config

contract="Vine.DataMan.RestfulService.ServiceContracts.IEntityCommonService"

behaviorConfiguration="webHttp"/>

注意加粗的文字。必须定义webHttp的行为。

原文:http://www.cnblogs.com/stragon/p/4718353.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值