Asp.net wcf various binding methods and message soap styles

Source: Internet
Author: User

Transmission Protocol: It can be http, https tutorial, tcp, named pipe, or msmq.

Message encoding: describes how to format a message. It can be binary, text, or mtom.

Message version: It can be soap1.1 or soap1.2. You can also set whether the message version supports the ws-addressing protocol. When operating with the non-. net platform, pay attention to the choice of message version.

Each binding has several binding elements. You can use the following code to view the default binding element and the message version used for binding.

Using system;
Using system. collections. generic;
Using system. text;
Using system. servicemodel;
Using system. servicemodel. channels;

Namespace wcfbinding
{
Class program
{
Static void main (string [] args)
{
Outputbindingelements (new basichttpbinding ());
Outputbindingelements (new wshttpbinding ());
Outputbindingelements (new nettcpbinding ());
Outputbindingelements (new netnamedpipebinding ());
Outputbindingelements (new netmsmqbinding ());

Console. readline ();
}
Static void outputbindingelements (binding)
{
Console. writeline ("binding: {0}", binding. gettype (). name );
Console. writeline ("messageversion: {0}", binding. messageversion );
Bindingelementcollection elements = binding. createbindingelements ();
Foreach (bindingelement element in elements)
{
Console. writeline ("{0}", element. gettype (). fullname );
}
Console. writeline ();
}
}
}


 

The result shows that only basichttpbinding uses soap1.1 and is addressingnone. Other bindings use the message version soap1.2 and addressing10. Below we will write some code to view the messages formatted in these two message formats. To easily change the message version, use the custombinding class to define custom binding:

Using system;
Using system. runtime. serialization;

Namespace fruitmodel
{
[Datacontract (namespace = "http://www.zhutiai.com")]
Public class fruit
{
Private string m_name = string. empty;
Private string m_price = string. empty;
Public fruit (string name, string price)
{
M_name = name;
M_price = price;
}
[Datamember]
Public string name
{
Get {return m_name ;}
Set {m_name = value ;}
}
[Datamember]
Public string price
{
Get {return m_price ;}
Set {m_price = value ;}
}
}
[Datacontract (namespace = "http://www.zhutiai.com")]
Public class fruitheader
{
Private string m_headerkey = string. empty;
[Datamember]
Public string headerkey
{
Get {return m_headerkey ;}
Set {m_headerkey = value ;}
}
Public fruitheader (string key)
{
M_headerkey = key;
}
}
}

 

 


Service contract:

Using system;
Using system. servicemodel;
Using system. servicemodel. channels;
Using fruitmodel;

Namespace ifruitservice
{
[Servicecontract (namespace = "http://www.zhutiai.com")]
Public interface ifruitpriceservice
{
[Operationcontract (action = "*", replyaction = "*")]
Message getfruit (message m );
}
}

 

 


Service implementation:

 

Using system;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. servicemodel. channels;
Using ifruitservice;
Using fruitmodel;

Namespace fruitpriceservice
{
Public class fruitpriceservice: ifruitpriceservice
{
Public message getfruit (message m)
{
Console. writeline (m. tostring ());
Message = message. createmessage (m. version, "*", new fruit ("banana", "6.00 "));
Messageheader mheader = messageheader. createheader ("fruitresponseheader ",
Http://www.zhutiai.com, new fruitheader ("response "));
Message. headers. add (mheader );
Return message;
}
}
}

 

 


Storage Service:

 

Using system;
Using system. servicemodel;
Using system. servicemodel. channels;
Using system. text;
Using ifruitservice;

Namespace wcfmessagehost
{
Class program
{
Static void main (string [] args)
{
Using (servicehost host = new servicehost (typeof (fruitpriceservice. fruitpriceservice ),
New uri ("http: // localhost: 8000 /")))
{
Textmessageencodingbindingelement textelement = new textmessageencodingbindingelement (
Messageversion. soap12wsaddressing10, encoding. utf8 );
Custombinding custombind = new custombinding ();
Custombind. elements. add (textelement );
Custombind. elements. add (new httptransportbindingelement ());

Host. addserviceendpoint (typeof (ifruitpriceservice), custombind, "fruitservice ");
Host. open ();
Console. writeline ("fruit service is running ...");
Console. readline ();
}
}
}
}

 

 


Client call:

 

Using system;
Using system. servicemodel;
Using system. servicemodel. channels;
Using system. runtime. serialization;
Using system. text;
Using ifruitservice;
Using fruitmodel;

Namespace wcfmessageclient
{
Class program
{
Static void main (string [] args)
{
Console. writeline ("fruit client is running ...");
Endpointaddress epaddr = new endpointaddress ("http: // localhost: 8000/fruitservice ");
Textmessageencodingbindingelement textelement = new textmessageencodingbindingelement (
Messageversion. soap12wsaddressing10, encoding. utf8 );
Custombinding custombind = new custombinding ();
Custombind. elements. add (textelement );
Custombind. elements. add (new httptransportbindingelement ());
Ifruitpriceservice proxy = channelfactory <ifruitpriceservice>. createchannel (custombind, epaddr );

Messageheader mheader = messageheader. createheader ("fruitheader", "http://www.zhutiai.com ",
New fruitheader ("password "));
Message = message. createmessage (messageversion. soap12wsaddressing10 ,"*",
New fruit ("apple", "4.00 "));
Message. headers. add (mheader );

Message m = proxy. getfruit (message );
Console. writeline (m. tostring ());
Console. readline ();
}
}
}

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.