WCF REST 的简单示例。
[ServiceContract]
public interface IService1
{
[WebGet(UriTemplate = "contact/{PID}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
Person GetContact(string PID);
//[WebInvoke(Method = "POST", UriTemplate = "person")]
[OperationContract]
[WebInvoke(UriTemplate = "/create", Method = "POST")]
void SaveContact(Person person);
[WebGet(UriTemplate = "/")]//RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)
[OperationContract]
List<Person> GetAllContacts();
[OperationContract]
[WebInvoke(UriTemplate = "/{id}", Method = "PUT")]
void UpdateProduct(string id, Person person);
[OperationContract]
[WebInvoke(UriTemplate = "/{id}", Method = "DELETE")]
void DeleteProduct(string id);
[OperationContract]
[WebInvoke(UriTemplate = "UploadFile")]
void UploadFile(Stream stream);
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract(Namespace = "")]
public class Person
{
[DataMember]
public string PersonId { get; set; }
[DataMember]
public string PersonName { get; set; }
}
Service Implementation :
public class Service1 : IService1
{
public Person GetContact(string PID)
{
return new Person(){PersonId=PID, PersonName=PID+".Name"};
}
public void SaveContact(Person person)
{
int a = 0;
}
public void UpdateProduct(string id, Person person)
{
int b = 0;
}
public List<Person> GetAllContacts()
{
List<Person> lstPerson = new List<Person>();
for (int i = 0; i < 10; i++)
{
Person p = new Person();
p.PersonId = i.ToString();
p.PersonName = i + "Person";
lstPerson.Add(p);
}
return lstPerson;
}
public void DeleteProduct(string id)
{
int c = 0;
}
public void UploadFile( System.IO.Stream stream)
{
object obj = stream;
}
}
Call Rest service using WebRequest :
class Program
{
static void Main(string[] args)
{
new Program().GetContacts();
//new Program().AddContacts();
//new Program().Update();
//new Program().Delete();
}
public void GetContacts()
{
WebRequest req = WebRequest.Create(@"http://localhost:60517/Service1.svc/");
req.Method = "GET";
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
if (resp.StatusCode == HttpStatusCode.OK)
{
using (Stream respStream = resp.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
Console.WriteLine(reader.ReadToEnd());
}
}
else
{
Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
}
Console.Read();
}
public void AddContacts()
{
WebRequest req = WebRequest.Create(@"http://localhost:60517/Service1.svc/create");
req.Method = "POST";
req.ContentType = @"application/xml; charset=utf-8";
WriteProductXml(req, "11", "Asif");
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
Console.Read();
}
public static void WriteProductXml(WebRequest req, string name, string description)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("<Person>");
builder.AppendLine("<PersonId>" + description + "</PersonId>");
builder.AppendLine("<PersonName>" + name + "</PersonName>");
builder.AppendLine("</Person>");
req.ContentLength = Encoding.UTF8.GetByteCount(builder.ToString());
using (Stream stream = req.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(builder.ToString()), 0, Encoding.UTF8.GetByteCount(builder.ToString()));
}
}
public void Update()
{
WebRequest req = WebRequest.Create(@"http://localhost:60517/Service1.svc/1");
req.Method = "PUT";
req.ContentType = @"application/xml; charset=utf-8";
WriteProductXmlUpdate(req, "11", "Asif");
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
Console.Read();
}
public static void WriteProductXmlUpdate(WebRequest req, string name, string description)
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("<Person>");
builder.AppendLine("<PersonId>" + description + "</PersonId>");
builder.AppendLine("<PersonName>" + name + "</PersonName>");
builder.AppendLine("</Person>");
req.ContentLength = Encoding.UTF8.GetByteCount(builder.ToString());
using (Stream stream = req.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(builder.ToString()), 0, Encoding.UTF8.GetByteCount(builder.ToString()));
}
}
public void Delete()
{
WebRequest req = WebRequest.Create(@"http://localhost:60517/Service1.svc/1");
req.Method = "DELETE";
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
Console.WriteLine(string.Format("Status Code: {0}, Status Description: {1}", resp.StatusCode, resp.StatusDescription));
Console.Read();
}
}