updating data through the rest Web service The REST Web Service provides an operation to query data in response to an HTTP GET request. However, the HTTP protocol supports other forms of requests that you can use to provide actions to modify the data in the rest Web service. The most common scenario is when you use an HTTP POST request to create a new project, an HTTP put request to update an existing data operation, and an HTTP delete request to define the action to delete the item. Note that the above conventions are not mandatory, and you can also update and delete data using HTTP POST requests, but this is not the recommended practice. The reason for this definition is that the HTTP protocol: The POST request is non-reciprocal, and the put request is equivalent to the delete request. This means that the put request and the delete request can invoke the operation multiple times, with the same effect: they appear to have been executed only once, but not for post. Therefore, if you use a put request to implement an update operation, you can perform the update operation repeatedly on the same data, and the results of these operations are always the same. This logic also applies to delete requests, and if an item has been deleted, it continues to be deleted. However, using a POST request to repeatedly add the same information may result in duplicate data. In a WCF Web service, you use the WebGet attribute class tag operation in response to an HTTP GET request; You also provide a template to specify the URI that the Web client can access to invoke the operation of the service. To support post requests for HTTP, put requests and delete requests, WCF provides the WebInvoke attribute class. Similarly, you use the attribute class to indicate a URI, and you can also indicate the type of action response request message. When the rest Web service receives a message that satisfies the type defined by the URI, the request invokes the operation. This system allows multiple operations to respond to the same URI, except that these operations expect different HTTP message types. This is quite useful and allows you to avoid thinking about how to use multiple URI systems to enable the same data to support different operations. For example, the GetCustomer operation in the Iproductssales service contract might be defined as: then you can use the same URI to define the DeleteCustomer operation to delete a customer, but you must specify that the operation responds to an HTTP delete message; In the following set of exercises, you will extend the Productssales REST Web Service so that the service has operations that can insert, update, and delete customer data. Exercise: Extend the Productssales service to support data Update 1. In Visual Studio, in text-editing modeOpen the IProductsSales.cs file under the Productssalesservice project. 2. Add the following action to the Iproductssales interface The Createcutomer method you will implement will add a new client to the AdventureWorks database. This operation marks the WebInvoke attribute because it is an insert operation, so the method property is set to post. The UriTemplate property specifies the details of the claims required to create a new customer (the fields in the AdventureWorks database are much more than that, but the other columns use their default values). As with the skip and top parameters in the Getallorders and Getallcustomer operations in the previous exercise, the order of UriTemplate contains parameters is not important, As long as you define the parameter in the CreateCustomer method with the same name as the parameter contained in Uritempalte. A Web program can submit a POST request to create a new customer through the URI specified by the UriTemplate property. For example, to add a record for John Sharp, the application will use the following uri:customers? Firstname=john&lastname=sharp&emailaddress=john@adventure-works.com&phone= (123) 456789 3. Add the following UpdateCustomer action to the Iproductssales interface: The UpdateCustomer method modifies the value that the specified customer number corresponds to the customer's e-mail address and phone number that is declared for the parameter. This action responds to HTTP put request 4. Add the DeleteCustomer action to the Iproductssales interface: The method responds to an HTTP delete request and then removes the specified customer from the database. 5. Open the ProductsSales.cs file in the Iproductssalesservice project in text-editing mode and implement the CreateCustomer method in the Productssales class: The above method creates a new contact object with the parameters passed in, and then uses the Entity Framework (EF) to persist the new object into the database. Be aware that some columns automatically generate their default values. The AdventureWorks database automatically generates the customer number for the new customer, and the CreateCustomer method returns the auto-generated ID. If an error occurs, the method throws an HTTP status code of 400 WebfaulteXception exception. 6. Add the following UpdateCustomer method to the Productssales class: The UpdateCustomer method first tries to find the customer that needs to be updated. When a matching customer is found, the method updates the value of the email parameter and the phone parameter to the customer object and saves it to the database. Note that if either parameter is NULL, the method will not update the corresponding field for the object, which allows the Web client program to omit one of these parameters from the corresponding field in the database. If a matching customer is not found from the AdventureWorks database, the method throws an HTTP status code of 404 for the webfaultexception exception. If an error occurs during an update, the method throws an Webfaultexception exception with an HTTP status code of 400. 7. Add the following DeleteCustomer method to the Productssales class: This method is similar to the Updatecustoomer method, but the goal of the method is to discover the matching customer and remove the customer from the database. If a matching customer is not found from the AdventureWorks database, the method throws an HTTP status code of 404 for the webfaultexception exception. If an error occurs during an update, the method throws an Webfaultexception exception with an HTTP status code of 400. 8. Regenerate the Productssalesservice project. Note that you are simply regenerating the Productssalesservice project instead of the entire solution, because at this point the Productssales client program cannot be successfully generated because CreateCustomer, The UpdateCustomer and DeleteCustomer methods have not yet been included in the client's lead. You will add these methods in the following exercise. You can invoke Createcustomer,updatecustomer and DeleteCustomer operations from a Web program by committing Post,put and delete requests. The following code fragment was extracted from an ASP. NET Web application, demonstrating how to submit an HTTP delete request to attempt to delete a customer with a customer code of 101 from the AdventureWorks database. You cannot simply enter a URI in the address bar of a Web browser such as IE and invoke the above action. This is because most browsers work by sending HTTP GET requests;Because the browser itself is for querying data, not for modifying the data. To test these new methods, you can create an ASP. NET Web program or invoke these operations through a procedural client program. In your next exercise, you will create a procedural client program to invoke the newly added operation. In order for the client to invoke the new operation, you must first update the Productssalesproxy class. Exercise: Update the client to test productssales REST Web Service 1. Open the ProductsSalesProxy.cs file for the Productssalesclient project in text-editing mode. Add Createcustomer,updatecustomer, and DeleteCustomer methods to the Productssalesproxy class: These methods use the same pattern as the methods defined in the previous section of the exercise They simply implemented routing requests to the service through the channel attribute of the Clientbase<iproductssales> class. Note that the UpdateCustomer method sets the default value for the parameter email and phone, because the client can omit either of these two parameters if necessary. 2. Open the Program.cs file under the Productssalesclient project in text-editing mode, and then add the following code before Proxy.close () in the Main method: The code above implements the test Productssales rest service by invoking the newly created method in turn. The first Test creates a new customer, and then calls the GetCustomer method using the customer number of the newly created customer to verify that the CreateCustomer operation was successful. The second test changes the customer's e-mail address, and then calls GetCustomer again for more information about the updated customer. The last test method deletes the newly created customer, and if the DeleteCustomer method succeeds then the GetCustomer method returns a null object after the delete operation is completed. 3. Rebuild Solution 4. Run the solution in non-adaptive mode. In the Productssalesclient console window, press the ENTER key. All new tests will be executed successfully: A new client is added, then updated, and then deleted: 5. Press the ENTER key in the Productssalesclient console window to close the client program, and then press ENTER in the Productssaleshost console to close the boarding program.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.