创建Wcf数据服务的事例

先创建实体类:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Data.Services.Common;

namespace Net.Wcf.WebApplication
{
    [DataServiceKey("Id")]
    public class Student
    {
        public string Id { get; set; }
        public string Name { get; set; }

        public Student() { }

        public Student(string id,string name) {
            this.Id = id;
            this.Name = name;
        }
    }

    [DataServiceKey("Id")]
    public class Teacher
    {
        public string Id { get; set; }
        public string Name { get; set; }

        public Teacher() { }

        public Teacher(string id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}

创建提供数据服务类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Net.Wcf.WebApplication
{
    public class DataServiceModel
    {
        #region 方法
        public IQueryable<Student> Students {
            get {
                return GetAllStudents().AsQueryable();
            }
        }

        public IQueryable<Teacher> Teachers
        {
            get
            {
                return GetAllTeachers().AsQueryable();
            }
        }

        #endregion

        #region 提供数据
        private List<Student> GetAllStudents() {
            return new List<Student>() {
              new Student("1","刘备"),new Student("2","关羽"),new Student("3","张飞"),new Student("4","赵云"),
              new Student("5","曹操"),new Student("6","周瑜"),new Student("7","孙权"),new Student("8","司马懿"),

            };
        }

        private List<Teacher> GetAllTeachers()
        {
            return new List<Teacher>() {
              new Teacher("1","李世民"),new Teacher("2","赵匡胤"),new Teacher("3","赵构"),new Teacher("4","岳飞"),
              new Teacher("5","朱元璋"),new Teacher("6","朱棣"),new Teacher("7","乾隆"),new Teacher("8","蒋介石"),

            };
        }
        #endregion
    }
}

 

 

配置服务:

 

发布服务,就可以在浏览器查看了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值