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 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数据的处理方法的更多相关文章
- 小程序post请求,后台接收不到数据的解决方法
wx.request({ url: 'myurl', method:'POST', dataType:'json', data: { mydata:mydata }, success(res) { c ...
- WCF学习之旅—WCF寄宿前的准备(八)
一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类协定的定 ...
- WCF学习之旅—WCF服务部署到IIS7.5(九)
上接 WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...
- WCF入门教程3——WCF通信模式
本章内容 请求/响应模式 单工模式 双工模式 WCF异步调用 请求与响应模式 请求/响应 请求/响应通信是指客户端向服务端发送消息后,服务端会向客户端发送响应.这也意味着在接收到服务的响应以前 ...
- WCF入门教程1——WCF简要介绍
什么是WCF Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windo ...
- WCF初探-26:WCF中的会话
理解WCF中的会话机制 在WCF应用程序中,会话将一组消息相互关联,从而形成对话.会话”是在两个终结点之间发送的所有消息的一种相互关系.当某个服务协定指定它需要会话时,该协定会指定所有调用(即,支持调 ...
- WCF初探-11:WCF客户端异步调用服务
前言: 在上一篇WCF初探-10:WCF客户端调用服务 中,我详细介绍了WCF客户端调用服务的方法,但是,这些操作都是同步进行的.有时我们需要长时间处理应用程序并得到返回结果,但又不想影响程序后面代码 ...
- WCF初探-15:WCF操作协定
前言: 在前面的文章中,我们定义服务协定时,在它的操作方法上都会加上OperationContract特性,此特性属于OperationContractAttribute 类,将OperationCo ...
- WCF初探-22:WCF中使用Message类(上)
前言 从我们学习WCF以来,就一直强调WCF是基于消息的通信机制.但是由于WCF给我们做了高级封装,以至于我们在使用WCF的时候很少了解到消息的内部机制.由于WCF的架构的可扩展性,针对一些特殊情况, ...
随机推荐
- Android常见控件— — —EditText
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=" ...
- apply()和call()和bind()
1.方法定义 call, apply都属于Function.prototype的一个方法,它是JavaScript引擎内在实现的,因为属于Function.prototype,所以每个Function ...
- Why one-norm is an agreeable alternative for zero-norm?
[转载请注明出处]http://www.cnblogs.com/mashiqi Today I try to give a brief inspection on why we always choo ...
- 转 TextBox的EnableViewState属性问题
问题如下: ---------------------------------------------------------------------------------------------- ...
- excel导入导出
using System; using System.Collections.Generic; using System.Linq; using System.Text; using FS.Exten ...
- 排序算法总结(四)快速排序【QUICK SORT】
感觉自己这几篇都是主要参考的Wikipedia上的,快排就更加是了....wiki上的快排挺清晰并且容易理解的,需要注意的地方我也添加上了注释,大家可以直接看代码.需要注意的是,wikipedia上快 ...
- Qt 自定义 滚动条 样式
今天是时候把软件中的进度条给美化美化了,最初的想法就是仿照QQ. 先前的进度条是这样,默认的总是很难受欢迎的:美化之后的是这样,怎么样?稍微好看一点点了吧,最后告诉你实现这个简单的效果在Qt只需要加几 ...
- 数迹学——Asp.Net MVC4入门指南(4):添加一个模型
一.添加模型类 二.添加MovieDBContext类,连接数据库 DbContext类继承自 System.Data.Entity; 负责在数据库中获取,存储,更新,处理实例 MovieDBCont ...
- Nginx日志导入到Hive0.13.1,同步Hbase0.96.2,设置RowKey为autoincrement(ID自增长)
---------------------------------------- 博文作者:迦壹 博客地址:Nginx日志导入到Hive,同步Hbase,设置RowKey为autoincrement( ...
- nova-scheduler start flow