在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 in operation UpdateOrInsertEntityResponse to be a stream the operation

 must have a single parameter whose type is Stream.

如何解决。

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

将原始的

<%@ ServiceHost Language="C#" Debug="true" Service="Vine.DataMan.RestfulService.EntityService"

CodeBehind="EntityService.svc.cs" %>

增加新的属性:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

最后结果是:

<%@ ServiceHost Language="C#" Debug="true" Service="Vine.DataMan.RestfulService.EntityService"

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

修改配置文件Web.config

<system.serviceModel>

<services>

<service name="Vine.DataMan.RestfulService.EntityService">

<endpoint binding="webHttpBinding"

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

behaviorConfiguration="webHttp"/>

</service>

</services>

<behaviors>

<endpointBehaviors>

<behavior name="webHttp">

<webHttp/>

</behavior>

</endpointBehaviors>

<serviceBehaviors>

<behavior>

<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

<serviceMetadata httpGetEnabled="true"/>

<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->

<serviceDebug includeExceptionDetailInFaults="true"/>

</behavior>

</serviceBehaviors>

</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

</system.serviceModel>

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

WCF使用小结:(1)WCF接收HTTP POST数据的处理方法的更多相关文章

  1. 小程序post请求,后台接收不到数据的解决方法

    wx.request({ url: 'myurl', method:'POST', dataType:'json', data: { mydata:mydata }, success(res) { c ...

  2. WCF学习之旅—WCF寄宿前的准备(八)

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类协定的定 ...

  3. WCF学习之旅—WCF服务部署到IIS7.5(九)

    上接   WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...

  4. WCF入门教程3——WCF通信模式

    本章内容 请求/响应模式 单工模式 双工模式 WCF异步调用 请求与响应模式 请求/响应     请求/响应通信是指客户端向服务端发送消息后,服务端会向客户端发送响应.这也意味着在接收到服务的响应以前 ...

  5. WCF入门教程1——WCF简要介绍

    什么是WCF Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windo ...

  6. WCF初探-26:WCF中的会话

    理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...

  7. WCF初探-11:WCF客户端异步调用服务

    前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...

  8. WCF初探-15:WCF操作协定

    前言: 在前面的文章中,我们定义服务协定时,在它的操作方法上都会加上OperationContract特性,此特性属于OperationContractAttribute 类,将OperationCo ...

  9. WCF初探-22:WCF中使用Message类(上)

    前言 从我们学习WCF以来,就一直强调WCF是基于消息的通信机制.但是由于WCF给我们做了高级封装,以至于我们在使用WCF的时候很少了解到消息的内部机制.由于WCF的架构的可扩展性,针对一些特殊情况, ...

随机推荐

  1. Rhel6-mysql_cluster配置文档

    MySQL Cluster 是一种技术,其主要功能是在无共享的相关系统中部署内存中数据库 的 Cluster .在通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求. 此外,由于每个组 ...

  2. 使用 HttpClient 和 HtmlParser 实现简易爬虫

    这篇文章介绍了 HtmlParser 开源包和 HttpClient 开源包的使用,在此基础上实现了一个简易的网络爬虫 (Crawler),来说明如何使用 HtmlParser 根据需要处理 Inte ...

  3. Jquery EasyUI Tree .net实例

    图片: 针对tree: 数据库: CREATE TABLE [dbo].[SystemModel]( [Id] [,) NOT NULL, [Name] [nvarchar]() NULL, [Fat ...

  4. 实际项目中积累的一些关于事件的简单应用JS代码段(能力有限,不喜轻喷,23333)

    1:鼠标移入移出显示另一张图片 var yuanquan_1 = document.getElementById("yuanquan_1" );  yuanquan_1. onmo ...

  5. windows调试器尝鲜

    曾几何时,我也下载过看雪论坛精华看的津津有味.可惜一直没有动手去调试,学到的x86汇编指令也忘得差不多了.最近将老机器的T4200 CPU换成了更省电,温度更低的P8800,为了支援新的VT虚拟化,特 ...

  6. decimalFormat(小数格式)

    这个格式是用来形容小数的,所以只对小数部分起作用 0 一个数字 # 一个数字,不包括 0 (0和#就是一个占位符,有几个就意味着要显示多少位,区别是0 匹配任意数字,#匹配不包括0的任意数字(最后的0 ...

  7. 552 you must authentication

    配置邮箱到outlook时 出现以下错误: 发送测试电子邮件消息: 无法发送此邮件.请在帐户属性中验证电子邮件地址.  响应服务器: 552 you must authentication 需要在”其 ...

  8. LintCode Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  10. 编译llvm+clang

    第一步,下载llvm代码: git clone git@github.com:llvm-mirror/llvm.git 第二步,进入llvm/tools目录并下载clang代码 cd llvm/too ...