传输协议:可以是http、https教程、tcp、命名管道或msmq等。
消息编码:描述消息如何格式化。可以是binary、text、mtom。
消息版本:可以是soap1.1、soap1.2。消息版本还可以设置是否支持ws-addressing协议。在与非.net平台进行互操作时,应该注意消息版本的选择。
每个绑定都有若干个绑定元素组成,可以用以下代码查看默认绑定元素以及绑定所用的消息版本。
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 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();
}
}
}
从结果可以看出,只有basichttpbinding使用soap1.1而且是addressingnone。其他绑定使用的都是soap1.2,addressing10的消息版本。下面就写一些代码,查看下这两种消息格式格式化后的消息。为了方便的更换消息版本,使用custombinding类定义自定义绑定:
using system;
using system.runtime.serialization;namespace fruitmodel
{
[datacontract(namespace = "http://www.111com.net")]
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.111com.net")]
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;
}
}
}
服务契约:using system;
using system.servicemodel;
using system.servicemodel.channels;
using fruitmodel;namespace ifruitservice
{
[servicecontract(namespace = "http://www.111com.net")]
public interface ifruitpriceservice
{
[operationcontract(action="*",replyaction="*")]
message getfruit(message m);
}
}
服务实现:
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=message.createmessage(m.version, "*", new fruit("banana", "6.00"));
messageheader mheader = messageheader.createheader("fruitresponseheader",
"http://www.111com.net", new fruitheader("response"));
message.headers.add(mheader);
return message;
}
}
}
寄存服务:
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();
}
}
}
}
客户端调用:
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.createchannel(custombind, epaddr); messageheader mheader = messageheader.createheader("fruitheader", "http://www.111com.net",
new fruitheader("password"));
message 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();
}
}
}