创Wcf案例数据服务

时间:2021-11-27 15:10:27

首先,创建实体类:

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

    }

}

配置服务:

创Wcf案例数据服务

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

公布服务,就能够在浏览器查看了。

创Wcf案例数据服务

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

版权声明:本文博主原创文章,博客,未经同意不得转载。