我使用基于此article的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>
有人给我一些建议吗? THX。
修改 我已经针对RESTService类运行了一个调试,发现每次我调试到CreatePerson函数时,我都得到了Adress和Age的null实体。 我想也许在客户端流写中可能有问题。