当前位置: 首页 > news >正文

WCF Data Services客户端访问

上一篇http://www.cnblogs.com/2018/archive/2010/10/17/1853384.html 

讲述了查询的相关语法和例子,如果在程序中如何使用这些发布的服务呢?下面对在代码中访问这些服务的方法进行一下汇总

客户端访问

查询

这些查询中可以结合上文的查询语法等使用

Ø 浏览器地址:输入地址,GET请求直接进行

Ø JavaScipt库:如ExtJS、DOJO、MS AJAX等支持JSON处理的JS库

Ø Service Reference引用

常用的形式,IDE直接添加引用,使用代理对象和上下文处理

//Generic泛型
                    {
                           DataServiceContext ctx = new DataServiceContext(u);
                           var q = ctx.Execute<Order>(new Uri("/Orders(10402)", UriKind.Relative));
                           foreach (var t in q)
                           {
                                  Console.WriteLine(t.OrderDate);
                           }
                    }
              
//Client Proxy代理
                    {
                           NorthwindEntities ctx = new NorthwindEntities(u);
                           var q = from c in ctx.Suppliers select c;
                           foreach (var t in q)
                           {
                                  Console.WriteLine(t.City);
                           }

Ø HTTP协议支持处理

       HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + "/Orders(10402)");
                           req.Method = "GET";
                           req.Accept = "application/json";
                           using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                           {
                                  using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                                  {
                                         Console.WriteLine(sr.ReadToEnd());
                                  }
                           }
 
                           req = (HttpWebRequest)WebRequest.Create(url + "/Orders(10402)");
                           req.Method = "GET";
                           req.Accept = "application/atom+xml";
                           using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                           {
                                  using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                                  {
                                         XmlDocument xml = new XmlDocument();
                                         xml.Load(sr);
 
                                         Console.WriteLine(xml.InnerXml);
                                  }
                           }

增删改

Ø HTTP形式

参考MSDN的规定传递参数和请求形式即可,处理有些麻烦

Ø Service Reference引用

比较常用的形式,具体参考“修改数据”一节

需要处理并发冲突:

DataServiceRequestException

· Entity Framework provider - In the data model, the ConcurrencyMode attribute of a property that is part of the concurrency token for an entity type is set to Fixed.

· Reflection provider - The ETagAttribute is applied to the data class that is an entity type. This attribute declares the concurrency token based on the supplied property names.

修改数据

Insert

HTTP POST

Content-Type:需正确设置

Update

HTTP Put/Merger

Delete

HTTP Delete

例子
需要修改数据对象,注意服务端需要设置正确的属性,如:
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;

客户端通过代理访问:

//Client Proxy

{

NorthwindEntities ctx = new NorthwindEntities(u);

var q = from c in ctx.Suppliers select c;

foreach (var t in q)

{

Console.WriteLine(t.City);

}

///添加

var cust = new Customer()

{

CustomerID = "Test1",

Address = "Beijing",

City = "Peking",

CompanyName = "demo",

ContactName = "test",

ContactTitle = "mr.",

Country = "China",

Fax = "123",

Phone = "111",

PostalCode = "456",

Region = "HD"

};

ctx.AddToCustomers(cust);

ctx.SaveChanges();

///更新

var upCust = (from c in ctx.Customers where c.CustomerID == cust.CustomerID select c).FirstOrDefault();

upCust.ContactName += "-UPD";

ctx.UpdateObject(upCust);

ctx.SaveChanges();

///删除

upCust = (from c in ctx.Customers where c.CustomerID == cust.CustomerID select c).FirstOrDefault();

ctx.DeleteObject(upCust);

ctx.SaveChanges();

可见,在客户端可以使用LINQ语法进行查询

除了客户端,服务端也有很强的支持,下文再说。

相关文章:

  • css知多少(4)——解读浏览器默认样式
  • Breakthrough—JavaScript基础
  • 辛苦几个小时,终于装完主机了
  • 【连载】【FPGA黑金开发板】Verilog HDL那些事儿--PS2封装(十八)
  • android httpClient 支持HTTPS的2种处理方式
  • 一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇阶段总结与WinForm篇展望...
  • 如何开启常用端口和其他端口
  • C# HttpRequest基础连接已经关闭: 接收时发生意外错误
  • 学习笔记----安装nginx
  • 从零开始学android开发-查看sqlite数据库
  • 大数据的导入与导出,可以用到两个方法
  • 各种类型Android源代码
  • 捷径系列:NSDate
  • Hyper-V虚拟化测试-博文地址汇总-更新中...
  • 2010年11月网络规划设计师考试试卷点评与思考
  • canvas绘制圆角头像
  • download使用浅析
  • eclipse(luna)创建web工程
  • PHP CLI应用的调试原理
  • Python socket服务器端、客户端传送信息
  • React 快速上手 - 07 前端路由 react-router
  • XForms - 更强大的Form
  • 阿里云前端周刊 - 第 26 期
  • 高性能JavaScript阅读简记(三)
  • 基于HAProxy的高性能缓存服务器nuster
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 用Node EJS写一个爬虫脚本每天定时给心爱的她发一封暖心邮件
  • 从如何停掉 Promise 链说起
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 资深实践篇 | 基于Kubernetes 1.61的Kubernetes Scheduler 调度详解 ...
  • ​你们这样子,耽误我的工作进度怎么办?
  • ​软考-高级-系统架构设计师教程(清华第2版)【第15章 面向服务架构设计理论与实践(P527~554)-思维导图】​
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (二)构建dubbo分布式平台-平台功能导图
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (剑指Offer)面试题34:丑数
  • (六)vue-router+UI组件库
  • (论文阅读30/100)Convolutional Pose Machines
  • (七)理解angular中的module和injector,即依赖注入
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • ./和../以及/和~之间的区别
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .NET Remoting学习笔记(三)信道
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .Net中的集合
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • @Conditional注解详解
  • [ C++ ] STL_list 使用及其模拟实现
  • [BUG]vscode插件live server无法自动打开浏览器
  • [Google Guava] 2.1-不可变集合