我根据这篇文章使用 WCF 编写了一个 RESTFul 接口。服务器端代码如下:======这是接口=======
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace RESTFulDaemon
{
[ServiceContract]
public interface IRestSerivce
{
//POST operation
[OperationContract]
[WebInvoke(UriTemplate = "", Method = "POST")]
Person CreatePerson(Person createPerson);
//Get Operation
[OperationContract]
[WebGet(UriTemplate = "")]
List<Person> GetAllPerson();
[OperationContract]
[WebGet(UriTemplate = "{id}")]
Person GetAPerson(string id);
//PUT Operation
[OperationContract]
[WebInvoke(UriTemplate = "{id}", Method = "PUT")]
Person UpdatePerson(string id, Person updatePerson);
//DELETE Operation
[OperationContract]
[WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
void DeletePerson(string id);
}
}
=======这是服务====================
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.ServiceModel;
namespace RESTFulDaemon
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class RESTService:IRestSerivce
{
List<Person> persons = new List<Person>();
public Person CreatePerson(Person person)
{
persons.Add(person);
return person;
}
public List<Person> GetAllPerson()
{
return persons.ToList();
}
public Person GetAPerson(string id)
{
return persons.FirstOrDefault(e => e.ID.Equals(id));
}
public Person UpdatePerson(string id, Person updatePerson)
{
Person p = persons.FirstOrDefault(e => e.ID.Equals(id));
p.Name = updatePerson.Name;
p.Age = updatePerson.Age;
return p;
}
public void DeletePerson(string id)
{
persons.RemoveAll(e => e.ID.Equals(id));
}
}
}
=========下面是客户端代码================
private void btnCreate_Click(object sender, RoutedEventArgs e)
{
//Client entity
Person p = new Person();
p.ID = "1";
p.Name = "scy";
p.Age = "25";
p.Address = "China HeNan";
HttpWebRequest request = WebRequest.Create("http://localhost:11126/RESTService/") as HttpWebRequest;
request.KeepAlive = false;
request.Method = "POST";
StringWriter sw = new StringWriter();
XmlSerializer xs = new XmlSerializer(p.GetType());
xs.Serialize(sw,(object)p);
string result = sw.ToString();
//generate xml that required
result = result.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n",string.Empty);
Regex regex = new Regex("<Person.*?>");
Match m = regex.Match(result);
if (m.Success)
{
result = result.Replace(m.Value,"<Person xmlns=\"http://schemas.datacontract.org/2004/07/RESTFulDaemon\">");
}
byte[] buffer = Encoding.Default.GetBytes(result);
request.ContentLength = buffer.Length;
request.ContentType = "text/xml";
Stream PostData = request.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
StreamReader loResponseStream = new StreamReader(resp.GetResponseStream(), Encoding.Default);
string Response = loResponseStream.ReadToEnd();
MessageBox.Show(Response);
loResponseStream.Close();
resp.Close();
}
问题是:每次单击按钮时,我都会收到如下消息:
<Person xmlns="http://schemas.datacontract.org/2004/07/RESTFulDaemon">
<ID>1</ID>
<Name>scy</Name>
<Age>25</Age>
<Address>China HeNan</Address>
</Person>
但实际上我得到的结果如下:
<Person xmlns="http://schemas.datacontract.org/2004/07/RESTFulDaemon">
<Address i:nil="true" />
<Age i:nil="true" />
<ID>1</ID>
<Name>scy</Name>
</Person>
有人给我一些建议吗?谢谢。
编辑: 我对 RESTService 类进行了调试,发现每次调试 CreatePerson 函数时,我都会得到 Adress 和 Age 值为空的 person 实体。我认为客户端流写入可能有问题。