Пример #1
0
        public Customer AddCustomer(Customer customer)
        {
            lock (writeLock)
            {
                counter++;
                UriTemplate itemTemplate = WebOperationContext.Current.GetUriTemplate("GetCustomer");
                customer.Uri = itemTemplate.BindByPosition(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri, counter.ToString());
                customers[counter.ToString()] = customer;
                WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(customer.Uri);
            }

            return customer;
        }
Пример #2
0
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:33694/Customers");
            Console.WriteLine("Service is hosted at: " + baseAddress.AbsoluteUri);
            Console.WriteLine("Service help page is at: " + baseAddress.AbsoluteUri + "/help");
            Console.WriteLine("");

            Console.WriteLine("Adding some customers with POST:");
            Customer alice = new Customer("Alice", "123 Pike Place", null);
            Uri aliceLocation = PostCustomer(baseAddress, alice);

            Customer bob = new Customer("Bob", "2323 Lake Shore Drive", null);
            Uri bobLocation = PostCustomer(baseAddress, bob);

            Console.WriteLine("");

            Console.WriteLine("Using PUT to update a customer");
            alice.Name = "Charlie";
            alice.Uri = aliceLocation;
            PutCustomer(aliceLocation, alice);

            Console.WriteLine("");
            Console.WriteLine("Using GET to retrieve the list of customers");
            List<Customer> customers = GetCustomers(baseAddress);
            foreach (Customer c in customers)
            {
                Console.WriteLine(c.ToString());
            }

            Console.WriteLine("");
            Console.WriteLine("Using DELETE to delete a customer");
            DeleteCustomer(bobLocation);

            Console.WriteLine("");
            Console.WriteLine("Final list of customers: ");
            customers = GetCustomers(baseAddress);
            foreach (Customer c in customers)
            {
                Console.WriteLine(c.ToString());
            }

            Console.WriteLine("");

            Console.WriteLine("Press any key to terminate");
            Console.ReadLine();
        }
Пример #3
0
        public Customer UpdateCustomer(string id, Customer newCustomer)
        {
            if (!customers.ContainsKey(id))
            {
                throw new WebFaultException(HttpStatusCode.NotFound);
            }

            lock (writeLock)
            {
                customers[id] = newCustomer;
            }

            return newCustomer;
        }
Пример #4
0
 static Uri PostCustomer(Uri uri, Customer customer)
 {
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
     request.Method = "POST";
     request.ContentType = "application/xml";
     using (Stream requestStream = request.GetRequestStream())
     {
         customerSerializer.WriteObject(requestStream, customer);
     }
     HttpWebResponse response = (HttpWebResponse) request.GetResponse();
     using (Stream responseStream = response.GetResponseStream())
     {
         Customer createdItem = (Customer) customerSerializer.ReadObject(responseStream);
         Console.WriteLine(createdItem.ToString());
     }
     response.Close();
     return new Uri(response.Headers[HttpResponseHeader.Location]);
 }
Пример #5
0
 static void PutCustomer(Uri uri, Customer customer)
 {
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
     request.Method = "PUT";
     request.ContentType = "application/xml";
     using (Stream requestStream = request.GetRequestStream())
     {
         customerSerializer.WriteObject(requestStream, customer);
     }
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();
     using (Stream responseStream = response.GetResponseStream())
     {
         Customer updatedItem = (Customer)customerSerializer.ReadObject(responseStream);
         Console.WriteLine(updatedItem.ToString());
     }
     response.Close();
 }