[WCF REST] Web message body style (message body style)

Source: Internet
Author: User

For the Web HTTP programming model, the service contract as an operational method does not need to apply the OperationContractAttribute feature, only need to apply WebGetAttribute and WebInvokeAttribute features as needed. The former is for get HTTP methods, or for other HTTP methods. The properties of WebGetAttribute and WebInvokeAttribute bodystyle and isbodystylesetexplicitly are related to the topic "Web Message body style".

   1: [AttributeUsage (AttributeTargets.Method)]
   2:publicsealedclass Webgetattribute:attribute, Ioperationbehavior
   3: {
   4:     //Other Members
   5: Public     webmessagebodystyle bodystyle {get; set;}
   6: }
   
   8: [AttributeUsage (AttributeTargets.Method)]
   9:public sealed class Webinvokeattribute:attribute, Ioperationbehavior
  Ten: {
  One:     //other Members
  :      Public Webmessagebodystyle bodystyle {get; set;}
  13:}

The style of the message body is represented by an enumeration webmessagebodystyle with the following definition.

   1:public enum Webmessagebodystyle
   2: {
   3:     Bare,
   4:     Wrapped,
   5:     Wrappedrequest,
   6:     Wrappedresponse
   7:}

We know that the request message and reply message are encapsulated for the action method input parameter and the return value (output parameter and reference parameter), respectively. Bare in Webmessagebodystyle indicates that the body part of the request message and reply message contains only the serialized contents for the input and return values (output parameters and reference parameters), while wrapped wraps an "envelope" based on the current operation. The enumeration entries Wrappedrequest and Wrappedresponse are used to encapsulate the body of the request message and reply message separately.

The default value for the property bodystyle of WebGetAttribute and WebInvokeAttribute is bare. If the property is set to Wrappedrequest, the reply message body is still in bare style, and if the property is set to Wrappedresponse, the request message body is still in bare style. The read-only property of the Boolean type isbodystylesetexplicitly indicates whether the display setting is made for the property bodystyle.

Directory
First, Xml+bare
Second, xml+wrapped
Third, Json+bare
Iv. json+wrapped
V. Bare request message style restrictions on single input
Vi. Bare reply message style restrictions on a single output

First, Xml + Bare

We look at the examples presented earlier to see how the body of the request message and reply message is structured in different styles for different message formats (XML and JSON). We have now modified the webinvokeattribute of the Create operation method applied in the contract interface Iemployees to explicitly specify the format (XML) and body style (Bare) of the request message and reply message. The return type is also programmed with the employee from void and returned directly to the employee object that was created.

   1: [ServiceContract]
   2:publicinterface iemployees
   3: {
   4:     //Other Members
   5:     
   6:     
   7:     
   8:     Bodystyle = Webmessagebodystyle.bare)]
   9:     Employee Create (employee employee);
  Ten: }
  
  :publicclass Employeesservice:iemployees
  13: {
  :     //Other Members
  : Public     Employee Create (employee employee)
  :     {
  :         employees. ADD (employee);
  :         return employee;
  :     }
  : }

We add an employee named "Harry" with the service call for the code shown below.

   1:using (channelfactory<iemployees> ChannelFactory = new Channelfactory<iemployees> ("EmployeeService"))
   2: {
   3:     iemployees proxy = Channelfactory.createchannel ();
   4:     proxy. Create (new Employee
   5:     {
   6:         Id         "003",
   7:         Name       = "Harry",
   8:          Grade     "G9",
   9:         Department = "Administrative Department"
  :     });           
  11:}

For the service invocation as shown above, because the message format and the body style are both XML and bare, the body as the request message and reply message is simply an XML fragment generated after the employee object is serialized, as shown below.

   1: Request message body:
   2:<Employeexmlns= "http://www.artech.com/"
   3:   xmlns:i= "Http://www.w3.org/2001/XMLSchema-instance" >
   4:   < Department > Administration Department </Department>
   5:   <Grade>G9</Grade>
   6:   < Id >003</Id>
   7:   <Name> Harry </Name>
   8:</Employee>
   
  : reply message body:
  
  :   xmlns:i = "Http://www.w3.org/2001/XMLSchema-instance" >
  :   <Department> Administration Dept. </Department>
  :   < Grade > G9</Grade>
  :   <Id>003</Id>
  :   < Name > Harry </Name>
  : </Employee>

Second, Xml + wrapped

Now we have modified the contract interface slightly, setting the property bodystyle of the WebInvokeAttribute attribute applied on the action method create to wrapped.

   1: [ServiceContract]
   2:publicinterface iemployees
   3: {
   4:     //Other Members
   5:     
   6:     
   7:     
   8:     Bodystyle = webmessagebodystyle.wrapped)]
   9:     Employee Create (employee employee);
  Ten: }

For the same service invocation, the request message and reply message will have the principal content as shown below. We can see that the XML generated by the employee is serialized as a child of the <Create> element in the request message, and for the reply message, the root element name of the XML generated after the employee is serialized is Createresult, not < Employee>, while the entire <CreateResult> is embedded in the < Createresponse > element.

   1: Request message body:
   2:<Createxmlns= "http://tempuri.org/">
   3:   
   4:         xmlns:i = "Http://www.w3.org/2001/XMLSchema-instance" >
   5:     <a:Department> Administration Dept. </a:Department>
   6:     < A:grade > G9</a:grade>
   7:     <a:Id>003</a:Id>
   8:     < A:name > Harry </a:name>
   9:   </employee>
  Ten:</Create>
  
  : reply message body:
  : <createresponse xmlns= "http://tempuri.org/" >
  :   < Createresult xmlns:a = "http://www.artech.com/"
  :         xmlns:i= "Http://www.w3.org/2001/XMLSchema-instance" >
  :     < a:department > Administration Department </a:department>
  :     <a:Grade>G9</a:Grade>
  :     < A:id >003</a:id>
  :     <a:Name> Harry </a:Name>
  :   </ Createresult >
  : </CreateResponse>

Third, json+ Bare

Above we illustrate the message format for different styles of message body content differences in the case of XML, and now we are in the same way to discuss when the message format is JSON, the different styles of the message body in terms of the structure of the difference. As shown in the following code snippet, we set the message format and the body style of the service operation create to json and bareby modifying the contract interface.

   1: [ServiceContract]
   2:publicinterface iemployees
   3: {
   4:     //Other Members
   5:     
   6:     
   7:     
   8:     Bodystyle = Webmessagebodystyle.bare)]
   9:     Employee Create (employee employee);
  Ten: }

Also for the previous service invocation, the employee object, expressed in JSON, will be used directly as the body part of the request message and reply message, as shown below. (S1004)

   1: Request message body:
   2: {"Department":"Administrative Department","Grade":"G9","Id":"003" ,"Name":"Harry"}
   
   4: reply message body:
   5: {"Department": "Administrative department", "Grade": "G9", "Id": "003", "Name": "Harry"}

Iv. json+ Wrapped

We later demonstrated the structure of the JSON message format in the wrapped style, so we only need to set the Bodystyle property of the WebInvokeAttribute attribute applied on the Create action method to wrapped.

   1: [ServiceContract]
   2:publicinterface iemployees
   3: {
   4:     //Other Members
   5:     
   6:     
   7:     
   8:     Bodystyle = webmessagebodystyle.wrapped)]
   9:     Employee Create (employee employee);
  Ten: }

As the following code shows, because the request message and reply message are in the wrapped style, the JSON object that represents the employee ends up as the "employee" property and "Createresult" property of the final JSON object. (S1005)

   1: Request message body:
   2: {"employee": {"Department":"Administrative Department","Grade":"G9" ,"Id":"003","Name":"Harry"}}
   
   4: reply message body:
   5: {"Createresult": {"Department": "Administrative department", "Grade": "G9", "Id": "003", "Name": "Harry"}}

v. Bare request message style requirements for single input

For bare message body style, it means that the XML or JSON that is generated after the object is serialized is directly the body of the message, so it applies only to a single object. Specifically, only action methods with unique input parameters can set the theme style of the request message to bare.

   1: [ServiceContract (Namespace = "http://www.artech.com/")]
   2:publicinterface ICalculator
   3: {
   4:     [WebInvoke (bodystyle = webmessagebodystyle.bare)]
   5:     Double Add (double x,  double y);
   6: }

As shown above is the definition of the contract interface for our familiar computing services. The message body style is bare's Operation method create has two input parameters (x and Y), which throws the InvalidOperationException exception as shown when the contract interface is implemented, prompting the " Convention" ICalculator The action ' ADD ' specifies that you want to serialize multiple request body parameters, but there are no wrapper elements. If there is no wrapper element, at most one body parameter can be serialized. Please delete the extra body parameters, or set the Bodystyle property of Webgetattribute/webinvokeattribute to wrapped".

Vi. bare reply message style requirements for a single output

Because the reply parameter is the encapsulation of the return value, the reference parameter, and the output parameter, the body style of the reply message cannot be set to bare when the action method has a reference parameter or an output parameter.

   1: [ServiceContract (Namespace = "http://www.artech.com/")]
   2:publicinterface ICalculator
   3: {    
   4:     [WebInvoke (bodystyle = webmessagebodystyle.wrappedrequest)]
   5:     void Add (Double x,  double y, out double result);
   6: }

As an example of computing a service contract, we now return the result of the addition operation as an output parameter as above, and set the Bodystyle property of the WebInvokeAttribute attribute applied to the action method to Wrappedrequest, This means that the request message and reply message are in wrapped and bare styles, respectively. When we host a service facility that implements the contract interface, the InvalidOperationException exception is thrown, and the operation ' Add ' of the "Convention ' ICalculator ' is specified to specify at least one response body parameter that is not the return value of the operation. When the Bodystyle property of Webgetattribute/webinvokeattribute is set to Bare, only the return value is allowed. Please delete the extra response body parameter or set the Bodystyle property to wrapped".

[WCF REST] Web message body style (message body style)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.