// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.Net; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; namespace Ws.Rest.WCF { [DataContract(Name="t1",Namespace="")] public class T1 { [DataMember(Name="f1")] public int F1 { get; set; } [DataMember(Name="f2")] public string F2 { get; set; } public T1() : this(0, "") { } public T1(int f1, string f2) { this.F1 = f1; this.F2 = f2; } } [CollectionDataContract(Name="t1array",Namespace="")] public class T1Array : List { public T1Array() : this(new T1[0]) { } public T1Array(T1[] array) : base(array) { } } public class Test { public T1 GetOne(int f1) { // dummy implementation return new T1(f1, "getOne"); } public T1[] GetAll() { // dummy implementation T1[] res = new T1[3]; res[0] = new T1(1, "getAll #1"); res[1] = new T1(2, "getAll #2"); res[2] = new T1(3, "getAll #3"); return res; } public T1[] GetSome(int start, int finish) { // dummy implementation T1[] res = new T1[finish - start + 1]; for(int i = 0; i < res.Length; i++) { res[i] = new T1(start + i, "getSome #" + (start + i)); } return res; } public bool Save(T1 o) { // dummy implementation return o.F1 > 0 && o.F2.Length > 0; } public bool Delete(int f1) { // dummy implementation return f1 > 0; } } [ServiceContract] public interface ITestService { [OperationContract] [WebInvoke(UriTemplate="/t1/{f1}",Method="GET",BodyStyle=WebMessageBodyStyle.Bare)] T1 GetOne(string f1); // there is no GetAll as it is processed as GetSome with null query parameters [OperationContract] [WebInvoke(UriTemplate="/t1?start={start}&finish={finish}",Method="GET",RequestFormat=WebMessageFormat.Xml,BodyStyle=WebMessageBodyStyle.Bare)] T1Array GetSome(string start, string finish); [OperationContract] [WebInvoke(UriTemplate="/t1",Method="POST",BodyStyle=WebMessageBodyStyle.Bare)] T1 Save(T1 o); [OperationContract] [WebInvoke(UriTemplate="/t1/{f1}",Method="PUT")] void Update(string f1, T1 o); [OperationContract] [WebInvoke(UriTemplate="/t1/{f1}",Method="DELETE")] void Delete(string f1); [WebInvoke(UriTemplate="/t1",Method="OPTIONS")] void Option1(); [WebInvoke(UriTemplate="/t1/{f1}",Method="OPTIONS")] void Option2(string f1); } [ServiceBehavior] public class TestService : ITestService { private Test real = new Test(); public T1 GetOne(string f1) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); return real.GetOne(int.Parse(f1)); } public T1Array GetSome(string start, string finish) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); if(start == null && finish == null) { return new T1Array(real.GetAll()); } else { return new T1Array(real.GetSome(int.Parse(start), int.Parse(finish))); } } public T1 Save(T1 o) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); if(real.Save(o)) { return o; } else { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; return null; } } public void Update(string f1, T1 o) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); if(int.Parse(f1) == o.F1 && real.Save(o)) { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NoContent; } else { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; } } public void Delete(string f1) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); if(real.Delete(int.Parse(f1))) { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NoContent; } else { WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound; } } public void Option1() { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept"); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NoContent; } public void Option2(string f1) { WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept"); WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NoContent; } } }