首页 > 代码库 > 创建Wcf数据服务的事例
创建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
}
}
配置服务:
发布服务,就可以在浏览器查看了。
创建Wcf数据服务的事例