Preliminary Study on WCF-19: WCF Message Protocol, Preliminary Study on wcf-19 Protocol

Source: Internet
Author: User
Tags visual studio 2010

Preliminary Study on WCF-19: WCF Message Protocol, Preliminary Study on wcf-19 Protocol
WCFMessage Protocol Overview

 

  • When a WCF application is generated, developers usually pay close attention to the data structure and serialization issues, rather than the message structure carrying data. For these applications, it is easy to create data protocols for parameters or return values. However, it is equally important to have full control over the structure of a SOAP message as well as its content. This is especially true when interoperability is required or when security issues need to be specially controlled at the message or message level. In these cases, you can create a message contract so that you can specify the exact structure of the SOAP message.
  • Generally, it is enough to use data protocols to define the message architecture. However, it is sometimes necessary to precisely control the structure of SOAP messages transmitted over the network. In this case, the most common solution is to insert custom SOAP Headers. Another common solution is to define the security attributes of the message header and body, that is, to determine whether to digitally sign and encrypt these elements. Message-style operations provide such control.

 

Create a Message Protocol

 

  • A message style operation can have a maximum of one parameter and one return value. Both the parameter and return value types are message types. That is, these two types can be directly serialized into the specified SOAP message structure. This can be any type or Message type marked with MessageContractAttribute.
  • To define a message protocol for a type (that is, to define the ing between this type and the SOAP envelope), apply MessageContractAttribute to this type. Then, the MessageHeaderAttribute will be applied to the members of the type to be SOAP Headers, and MessageBodyMemberAttribute will be applied to the members of the SOAP body to be messages.
  • MessageHeaderAttribute and MessageBodyMemberAttribute can be applied to all fields, attributes, and events, regardless of whether these fields, attributes, and events are public, private, protected, or internal.

 

WCF Message Protocol example

 

  • The solution structure is as follows:

  

  • Engineering Structure Description:
using System;using System.ServiceModel;using System.Collections.Generic;using System.Runtime.Serialization;namespace Service{    [ServiceContract]    public interface IUserInfo    {        [OperationContract]        User GetInfo();    }    [MessageContract]    public class User    {        [MessageHeader]        public string OprationType { get; set; }        [MessageHeader]        public DateTime OperationTime { get; set; }        [MessageBodyMember]        public int ID { get; set; }        [MessageBodyMember]        public string Name { get; set; }        [MessageBodyMember]        public int Age { get; set; }        [MessageBodyMember]        public string Nationality { get; set; }    }}

The UserInfo. cs code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service{    public class UserInfo:IUserInfo    {        public User GetInfo()        {            User user = new User();            user.OprationType = "GET";            user.OperationTime = System.DateTime.Now;            user.Name = "JACK";            user.Age = 20;            user.ID = 1;            user.Nationality = "CHINA";            return user;        }    }}

 

2. Host: console application and service bearer program. Add a reference to the Assembly Service. Complete the following code to host the Service. The Program. cs code is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using Service; using System. serviceModel; namespace Host {class Program {static void Main (string [] args) {using (ServiceHost host = new ServiceHost (typeof (UserInfo) {host. opened + = delegate {Console. writeLine ("the service has been started. Press any key to terminate! ") ;}; Host. Open (); Console. Read ();}}}}View Code

The App. config code is as follows:

<? Xml version = "1.0"?> <Configuration> <system. serviceModel> <services> <service name = "Service. userInfo "behaviorConfiguration =" mexBehavior "> The svcutil.exe tool is used to generate the client proxy class and client configuration files, Note: The client proxy type generated here is the message protocol type, so you need Svcutil.exe Append/mc To generate the client proxy class of the Message Protocol.

Svcutil.exe is a command line tool located in the path C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin. You can run the command line tool to generate a client proxy class.

  • Run cmd to open the command line and enter cd C: \ Program Files (x86) \ Microsoft SDKs \ Windows \ v7.0A \ Bin
  • Input svcutil.exe/out: f: \ UserInfoClient. cs/config: f: \ App. config http: // localhost: 1234/UserInfo/mc

 

3. Client: console application and Client calling program. Copy the generated UserInfoClient. cs and App. config to the Client project directory to complete the Client call code. The Code of Program. cs is as follows:

using System;namespace Client{    class Program    {        static void Main(string[] args)        {            UserInfoClient proxy = new UserInfoClient();            User user = proxy.GetInfo(new GetInfoRequest());            Console.WriteLine("{0,-10}{1,-20}{2,-10}{3,-10}{4,-10}{5,-10}",                "Type","Time","ID", "Name", "Age", "Nationality");                Console.WriteLine("{0,-10}{1,-20}{2,-10}{3,-10}{4,-10}{5,-10}",                  user.OprationType.ToString(),                  user.OperationTime.ToString(),                  user.ID.ToString(),                  user.Name.ToString(),                  user.Age.ToString(),                  user.Nationality.ToString());                        Console.Read();        }    }}

The program running result is as follows:

  

 

Summary:

 

  • Through the example, we have created and called the message protocol. Next, we can use the WCF client test program to view the location of the Message Protocol in the message. In the Start Menu, find the visual studio command prompt tool in the Visual studio Tools installation directory of visual studio 2010, enter the wcftestclient command, and add the service address http: // localhost: 1234/UserInfo/reference. After the call is successfully added, click the call button of the GetInfo method to view the message returned by the call. The OprationType and OperationTime attributes are displayed in the message header, while other attributes are displayed in the message body.

  

 

  

  • When generating the message client proxy class, We append a/mc parameter. When we view the client proxy class UserInfoClient. the cs Code indicates that the message Protocols generated by the client are modified by MessageContractAttribute, MessageHeaderAttribute, and MessageBodyMemberAttribute according to the definitions of the Server Message protocols. However, the GetInfo parameter of the operation protocol has an additional parameter of the type GetInfoRequest. Therefore, the GetInfo method can be called only when the client calls a new GetInfoRequest parameter type.

Related Article

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.