WCF(Codefirst)
Codefirst与Contractfirst相对应,先对代码进行编写,然后进行服务的定义!
项目一:类库项目。先定义完相应的数据实体,然后定义服务。
[DataContract(Name = "People")]
public class People//数据实体类
{
private string _name;
private int _age;
private string _hometown;
[DataMember(Name = "P_Name")]
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
[DataMember(Name = "P_Age")]
public int Age
{
get
{
return _age;
}
set
{
_age = value;
}
}
[DataMember(Name = "P_Hometown")]
public string Hometown
{
get
{
return _hometown;
}
set
{
_hometown = value;
}
}
}
[ServiceContract]
public interface IPeopleContract //定义服务
{
[OperationContract]
void SetMan(People man);
[OperationContract]
People GetMan();
}
public class PeopleContract : IPeopleContract //针对服务的实现
{
People _man;
#region IPeopleContract 成员
public void SetMan(People man)
{
_man = man;
}
public People GetMan()
{
return _man;
}
#endregion
}