传输协议:可以是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();
}
}
}
茶杯头甜蜜终章dlc 官方手机版v1.0.0.3
下载火柴人传说暗影格斗内置菜单 最新版v3.0.1
下载荒野乱斗测试服 安卓版v61.10.3
下载荒野乱斗彩虹服 安卓版v61.10.3
下载寒霜启示录 安卓版v1.25.10
寒霜启示录是一款生存模拟游戏,不少玩家可能对于末日都有着自己
末日城堡免广告版 安卓最新版v0.7.1
末日城堡免广告版是一款非常好玩的模拟经营类游戏,内部可以不看
甜蜜人生模拟器 最新版v1.4.5
甜蜜人生模拟器是一款非常好玩的模拟恋爱手游,玩家在这里能够对
武器锻造师内置功能菜单 v10.4
武器锻造师内置菜单版是游戏的破解版本,在该版本中为玩家提供了
开放空间overfield 安卓版v1.0.5
开放空间Overfield是一款箱庭养成经营手游,让你在广阔