WCF返回JSON及EXT调用WCF
差不多整了一天,这个东东刚开始看,非常好的框架,只是配置麻烦些,声明性编程,一时转不过弯弯来,声明的特性类慢慢一个一个了解吧
目标是EXT调用(POST/GET)WCF,数据均以JSON格式传输,理解框架是代码自动生成三层框架+WCF+EXT,
WCF返回JSON的方式,网上流传些两种方式,经测试都可以达到效果,分别是
第一种,dudu大侠的做法是用特性类声明,如
[OperationContract, WebInvoke( Method = "POST", ResponseFormat = WebMessageFormat.Json )] public IList<BA_MenuEntity> DoWork( BA_MenuEntity e ) { return BA_MenuBLL.Singleton.Select( new BA_MenuEntity() { Parent = 0 } ); }
其中ResponseFormat = WebMessageFormat.Json 这一行就可以,比较简单
第二种,后者成峰大侠的做法是利用配置实现返回JSON,具体操作如下
1,添加配置节点,自定义绑定
<customBinding> <binding name="JsonMapper"> 此处配置相当重要,使用了我们编写的JsonContentTypeMapper类,约定返回值类型是Json <webMessageEncoding webContentTypeMapperType="Microsoft.Ajax.Samples.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> </webMessageEncoding> <httpTransport manualAddressing="true"/> </binding> </customBinding>
2,写代码继承自WebContentTypeMapper,并在配置中指定此类的子类
public class JsonContentTypeMapper : WebContentTypeMapper {
public override WebContentFormat GetMessageFormatForContentType( string contentType ) {
return WebContentFormat.Json;
}
}
-----------------------------------------------------华丽的分隔线--------------------------------------------------------------
个人比较喜欢前者,dudu大侠的做法,简单些
遇到的其它的问题
1,EXT调用WCF时的传参,如上边的代码,DoWork接收一个自定义实体,该自定义实体比较娇情,它继承了其它的父类,so,一直无法正确返回JSON数据,最后得知:父类同样需要[DataContract]标注,如果该父类有虚属性,则子类必须指定override或new关键字,虽然VS可以编译通过,但WCF无法返回数据
2,出现method not allowed,这个简单,多半是POST/GET方法不对应,如客户端GET请求,而服务器端指定了仅支持POST就会报这个错
3,EXT的AJAX调用WCF时,比jQuery简单些,比dudu那篇简单,起码没有总结部分的3,5(1,2,3是必须的),注意使用EXT的jsonData配置,代码如下
var webpost = { "__type": "BA_MenuEntity:#Tstring.Core.Entity", "CnName": "信息发布", "EnName": "Information", "ExGUID": null, "ExInclude": false, "ExReamrk": null, "Ext": null, "ID": 1, "Name": null, "ExGuid": "9345E846-42B7-4811-9C0A-DE45257930FE", "Parent": 0, "Remark": "", "Url": "" }; Ext.Ajax.request({ url: '/service/defaultservice.svc/DoWork', //params: {}, jsonData: webpost, method: 'POST', success: function(response, options) { var responseArray = Ext.util.JSON.decode(response.responseText); if (responseArray != null) { Ext.Msg.alert('信息', response.responseText, true); } else { Ext.Msg.alert('失败', '调用失败'); } }, failure: function(conn, response, options) { Ext.Msg.alert('失败', conn.statusText); } });
-----------------------------------------------------华丽的分隔线--------------------------------------------------------------
也总结下,(代码环境:visual studio 2008,.NET3.5,EXT3.0)
1,新建ASP.NET WEB应用程序,添加EXT目录
2,新建"启用了AJAX的WCF服务",添加相关的特性类,代码看下来类似如下
namespace wcf2.Service { [ServiceContract( Namespace = "" )] [AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )] public class DefaultService { [OperationContract, WebInvoke( Method = "POST", ResponseFormat = WebMessageFormat.Json )] public IList<BA_MenuEntity> DoWork( BA_MenuEntity e ) { return BA_MenuBLL.Singleton.Select( new BA_MenuEntity() { Parent = 0 } ); } [OperationContract, WebInvoke( Method = "POST", ResponseFormat = WebMessageFormat.Json )] public string GetMenu() { return "HELLO WORLD!"; } } }
3,EXT调用WCF进行测试
最后也有个疑问:
WCF返回JSON,怎么再具体操作它返回的一些数据?如下代码所示:那个"d"是干嘛滴?怎么配置成别的名称?服务器端返回的类型为IList<BA_MenuEntity>,还有那个"__type":"BA_MenuEntity:#Tstring.Core.Entity",
{"d":[{"__type":"BA_MenuEntity:#Tstring.Core.Entity","CnName":"信息发布","EnName":"Information","ExGUID":null,"ExInclude":false,"ExReamrk":null,"Ext":null,"ID":1,"Name":null,"ExGuid":"9345E846-42B7-4811-9C0A-DE45257930FE","Parent":0,"Remark":"","Url":""}]}
-----------
2009-10-22后记
知道那个“d”是干嘛滴了,不过一直没有更新,为了以后复习,备注在此
[OperationContract( Name = "DoWork4" )] [WebInvoke( Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/DoWork4" )] [return: MessageParameter( Name = "e" )] public Person DoWork4( [MessageParameter( Name = "e" )] Person e ) { // 在此处添加操作实现 return new Person() { Age = 100, Name = "远始天尊" }; }
加重的两行,[return: MessageParameter( Name = "e" )]这一行影响返回JSON对象时的名称,就是传说中的"d",此时被声明为"e"了
后一行 public Person DoWork4( [MessageParameter( Name = "e" )] Person e ) {
这个影响的是方法接收JSON对象的参数名称,也被改为"e"了,呵呵
------------------------------------------
除非特别声明,文章均为原创,版权与博客园共有,转载请保留出处
BUY ME COFFEE


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架