和可以在客户端直接使用的查询对应,在服务端也有很多可以增强的功能

Service Operations

自己发布一些业务逻辑的处理

Service operations enable you to expose business logic in a data service, such as to implement validation logic, to apply role-based security, or to expose specialized querying capabilities.

具体要求如返回值、参数等参考MSDN,定义这些操作只要遵守这个要求即可

例子

实现自定义的条件查询

· http://localhost:12345/Northwind.svc/GetOrdersByCity?city='London'

· http://localhost:12345/Northwind.svc/GetOrdersByCity?city='London'&$top=2

[WebGet]
public IQueryable<Order> GetOrdersByCity(string city)
{
    if (string.IsNullOrEmpty(city))
    {
        throw new ArgumentNullException("city",                   
            "You must provide a value for the parameter'city'.");
    }
    // Get the ObjectContext that is the data source for the service.
    NorthwindEntities context = this.CurrentDataSource;
    try
    {
        var selectedOrders = from order in context.Orders.Include("Order_Details")
                             where order.Customer.City == city
                             select order;
         return selectedOrders;
    }
    catch (Exception ex)
    {
        throw new ApplicationException("An error occured: {0}", ex);
    }
}
Interceptors

拦截请求,这样可以加入自己的业务处理逻辑。目前系统预定义的有两个:

QueryInterceptor:由于限制查询的实体数据范围使用

ChangeInterceptor:修改实体时使用

WCF Data Services enables an application to intercept request messages so that you can add custom logic to an operation. You can use this custom logic to validate data in incoming messages. You can also use it to further restrict the scope of a query request, such as to insert a custom authorization policy on a per request basis.

Interception is performed by specially attributed methods in the data service. These methods are called by WCF Data Services at the appropriate point in message processing. Interceptors are defined on a per-entity set basis, and interceptor methods cannot accept parameters from the request like service operations can.

Query interceptor methods, which are called when processing an HTTP GET request, must return a lambda expression that determines whether an instance of the interceptor's entity set should be returned by the query results. This expression is used by the data service to further refine the requested operation.

例子
[QueryInterceptor("Orders")]
public Expression<Func<Order, bool>> OnQueryOrders()
{
    // Filter the returned orders to only orders // that belong to a customer that is the current user.return o => o.Customer.ContactName ==
        HttpContext.Current.User.Identity.Name;
}
 
[ChangeInterceptor("Products")]
public void OnChangeProducts(Product product, UpdateOperations operations)
{
    if (operations == UpdateOperations.Add ||
       operations == UpdateOperations.Change)
    {
        // Reject changes to discontinued products.if (product.Discontinued)
        {
            throw new DataServiceException(400,
                        "A discontinued product cannot be modified");
        }
    }
    else if (operations == UpdateOperations.Delete)
    {
        // Block the delete and instead set the Discontinued flag.
        throw new DataServiceException(400, 
            "Products cannot be deleted; instead set the Discontinued flag to 'true'"); 
    }
}
Data Service Provider

应用中我们一般使用的是Entity Framework provider[edmx],从数据库生成模型或则从模型建立数据库都可以

由于Provider模型的强大功能,当然我们也可以自己实现一个Provider,如下例,是一个类实现的实例:

namespace DataServices
{
       [DataServiceKey("Name")]
       public class Inductee
       {
              public string Name { get; set; }
              public bool Group { get; set; }
              public int YearInducted { get; set; }
              public List<Song> Songs { get; set; }
 
              public static List<Inductee> MakeInducteeList()
              {
                    return (new List<Inductee>()
              {
                    new Inductee()
                    {
                           Name = "Rolling Stones",
                           Group = false,
                           YearInducted = 1990,
                           Songs = new List<Song>()
                    },
                    new Inductee()
                    {
                           Name = "Beatles",
                           Group = false,
                           YearInducted = 1986,
                           Songs = new List<Song>()
                    }
              });
              }
       }
 
       [DataServiceKey("SongTitle")]
       public class Song
       {
              public string SongTitle { get; set; }
 
              public static List<Song> MakeSongList()
              {
                    return (new List<Song>()
              {
                    new Song(){SongTitle="Satisfaction"},
                    new Song(){SongTitle="All you need is love"},
              });
              }
       }
 
       public class AssignInducteesToSongs
       {
              public static void Assign(List<Inductee> inductee, List<Song> songs)
              {
                    inductee[0].Songs.Add(songs[0]);
                    inductee[1].Songs.Add(songs[1]);
              }
       }
       /// <summary>
       /// Summary description for MyDataModel
       /// </summary>
       public class MyDataModel
       {
              static List<Inductee> inductees;
              static List<Song> songs;
 
              static MyDataModel()
              {
                    inductees = Inductee.MakeInducteeList();
                    songs = Song.MakeSongList();
                    AssignInducteesToSongs.Assign(inductees, songs);
              }
 
              public IQueryable<Inductee> Inductees
              {
                    get
                    {
                           return inductees.AsQueryable();
                    }
              }
 
              public IQueryable<Song> Songs
              {
                    get
                    {
                           return songs.AsQueryable();
                    }
              }
       }
 
//服务
       [System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
       public class DemoOb : DataService<MyDataModel>
       {
              public static void InitializeService(DataServiceConfiguration config)
              {
                    config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                     config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
                   config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
              }
       }

这个服务发布后,就可以使用“WCF Data Services查询”的方法和客户端查询进行使用了。

该例子可以在http://dskit.codeplex.com 下载

Hosting

一般的服务承载在IIS中,以svc扩展名有IIS的HTTPHandler进行处理就行了

不过有时可能需要自己承载服务,此时就使用WCF的技术就行了

Using System.ServiceModel.Web;

 
WebServiceHost host = new WebServiceHost(typeof(SampleDataService));
host.Open();

其他的关于WCF的ABC只要在app.config中设置就行了

转载于:https://www.cnblogs.com/2018/archive/2010/10/20/1856388.html

WCF Data Services服务端处理汇总相关推荐

  1. 如何消费WCF Data Services定义的服务操作

    Service Operations (WCF Data Services)描述了如何 自定义WCF Data Service的服务.客户端如何消费可以参考文章Service Operations a ...

  2. WCF Data Services 基础

    把最近使用的WCF Data Service和WCF RIA Service的使用例子发布在站点http://dskit.codeplex.com , 本系列文章就把WCF Data Service和 ...

  3. 为什么微软要推 ADO.NET Data Services

    微软在 .NET 3.5 SP1 平台上,推了一组新的数据访问 Framework,叫做 ADO.NET Data Services.微软怕程序员太闲吗?为什么要创造 ADO.NET Data Ser ...

  4. 服务器响应回调函数,解决有关flask-socketio中服务端和客户端回调函数callback参数的问题(全网最全)...

    由于工作当中需要用的flask_socketio,所以自己学习了一下如何使用,查阅了有关文档,当看到回调函数callback的时候,发现文档里都描述的不太清楚,最后终于琢磨出来了,分享给有需要的朋友 ...

  5. jQuery中的ajax、jquery中ajax全局事件、load实现页面无刷新局部加载、ajax跨域请求jsonp、利用formData对象向服务端异步发送二进制数据,表单序列化(异步获取表单内容)

    jQuery中使用ajax: 在jQuery中使用ajax首先需要引入jQuery包,其引入方式可以采用网络资源,也可以下载包到项目文件中,这里推荐下载包到文件中:市面上有多个版本的jQuery库,这 ...

  6. 服务端json参数校验神器Json Schema

    目录 目录 json简介 服务端校验参数需求分析 json参数检验简单而繁琐方式 Json Schema Json Schema 入门 Json Schema 表达式 string Numeric t ...

  7. WCF 服务端+客户端动态调用

    最近在写WCF服务相关代码,把项目中用到的通讯框架做了下整理,以备以后自己记忆. WCF服务端: 包含契约定义:WCF.Contract.契约实现:WCF.Service 以及宿主主程序:WcfSer ...

  8. WCF服务端返回:(413) Request Entity Too Large

    出现这个原因我们应该都能猜测到,文件传出过大,超出了WCF默认范围,那么我们需要进行修改. 服务端和客户端都需要修改. 第一.客户端: <system.serviceModel><b ...

  9. WCF Data Service 的.NET Client 的不支持原生类型服务操作的解决方法

    WCF Data Service  的.NET Client 的不支持返回值为原生类型(string,int)的服务操作调用,例如我们用如下服务操作: [WebGet] public ObjectQu ...

最新文章

  1. MongoDB-GRIDFS大文件系统
  2. linux内核中的数据结构
  3. Spark Streaming介绍,DStream,DStream相关操作(来自学习资料)
  4. 深入剖析ThreadLocal实现原理以及内存泄漏问题
  5. 商汤研究院-SpringAutoML团队招聘啦~
  6. 10许可证即将到期_重要公告:这些企业,你们的证到期啦
  7. 商用平板 移动金融潜力巨大的应用平台
  8. Pandas系列(三)新增数据列
  9. IT人母亲的美国之行(3)
  10. matlab multisim,清华大学出版社-图书详情-《仿真软件教程——Multisim和MATLAB》
  11. 图解通信原理与案例分析-28:四大全球卫星导航系统GNSS的基本原理与技术对比---中国的北斗、美国的GPS、欧洲的伽利略、俄罗斯的格洛纳斯
  12. 通俗解读SGD、Momentum、Nestero Momentum、AdaGrad、RMSProp、Adam优化算法
  13. 女朋友让我深夜十二点催她睡觉,我用Python轻松搞定
  14. 软件测试-Mysql数据库3
  15. 恒指期货实盘记录及下周行情分析!
  16. 用定时器设计门铃,按下按键时蜂鸣器发出叮咚的门铃声
  17. c语言更改记事本改为大写,pdf英文字母小写改大写怎么改
  18. Arduino与Proteus仿真实例-LCD12864液晶显示屏(ST7920)驱动仿真
  19. ECCV 2022 | 新方案: 先剪枝再蒸馏
  20. 前端html通过鼠标操作进行样式的更改

热门文章

  1. leetcode - 617. 合并二叉树
  2. CREO - 基础2 - 如何让装配好的零件重新装配
  3. Mac 安装virtualbox 虚拟机用移动硬盘遇到的VERR_WRITE_PROTECT
  4. 【编撰】linux IPC 001 - 概述
  5. [GPL]GREP - basic - practice -advanced
  6. c语言计算机动画生成原理,计算机组成原理动画演示系统 - 源码下载|多媒体|源代码 - 源码中国...
  7. 163 邮件 服务器 ssl,为什么用163的smtp服务时要关闭TLS才能发送邮件?
  8. cmd运行python脚本处理其他文件_如何在cmd命令行里运行python脚本
  9. lc滤波电路电感电容值选择_滤波电容如何选择
  10. thinkphp extend.php,【ThinkPHP5.1】如何引用extend的类库