public Customer UpdateCustomer(string id, Customer newCustomer) { if (!customers.ContainsKey(id)) { WebOperationContext.Current.OutgoingResponse.SetStatusAsNotFound(); return null; } lock (writeLock) { customers[id] = newCustomer; } return newCustomer; }
public Customer AddCustomer(Customer customer) { lock (writeLock) { counter++; UriTemplateMatch match = WebOperationContext.Current.IncomingRequest.UriTemplateMatch; UriTemplate template = new UriTemplate("{id}"); customer.Uri = template.BindByPosition(match.BaseUri, counter.ToString()); customers[counter.ToString()] = customer; WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(customer.Uri); } return customer; }
static void Main(string[] args) { using (WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/Customers"))) { //WebServiceHost will automatically create a default endpoint at the base address using the WebHttpBinding //and the WebHttpBehavior, so there's no need to set it up explicitly host.Open(); Uri baseAddress = new Uri("http://localhost:8000/Customers"); using (WebChannelFactory<ICustomerCollection> cf = new WebChannelFactory<ICustomerCollection>( baseAddress )) { //WebChannelFactory will default to using the WebHttpBinding with the WebHttpBehavior, //so there's no need to set up the endpoint explicitly ICustomerCollection channel = cf.CreateChannel(); UriTemplate template = new UriTemplate("{id}"); Console.WriteLine("Adding some customers with POST:"); Customer alice = new Customer("Alice", "123 Pike Place", null); alice = channel.AddCustomer(alice); Console.WriteLine(alice.ToString()); Customer bob = new Customer("Bob", "2323 Lake Shore Drive", null); bob = channel.AddCustomer(bob); Console.WriteLine(bob.ToString()); Console.WriteLine(""); Console.WriteLine("Using PUT to update a customer"); alice.Name = "Charlie"; UriTemplateMatch match = template.Match(baseAddress, alice.Uri); alice = channel.UpdateCustomer(match.BoundVariables["id"], alice); Console.WriteLine(alice.ToString()); Console.WriteLine(""); Console.WriteLine("Using GET to retrieve the list of customers"); List<Customer> customers = channel.GetCustomers(); foreach (Customer c in customers) { Console.WriteLine(c.ToString()); } Console.WriteLine(""); Console.WriteLine("Using DELETE to delete a customer"); match = template.Match(baseAddress, bob.Uri); channel.DeleteCustomer(match.BoundVariables["id"]); Console.WriteLine(""); Console.WriteLine("Final list of customers: "); customers = channel.GetCustomers(); foreach (Customer c in customers) { Console.WriteLine(c.ToString()); } Console.WriteLine(""); } Console.WriteLine("Press any key to terminate"); Console.ReadLine(); } }