WCF配置详解
前面一篇文章《WCF 学习总结1 -- 简单实例》 一股脑儿展示了几种WCF部署方式,其中配置文件(App.config/Web.config)都是IDE自动生成,省去了我们不少功夫。现在回过头来看看IDE提供的Wcf Service Library项目模板中的默认服务端配置文件--App.config里面究竟有什么秘密。
服务端的配置文件主要是对services、bindings、behaviors的配置。在默认的App.config中,使用的是WCF Framework定义好的wsHttpBinding默认配置,所以看不到binding配置节。

配置节展开如下图:

BTW: "元数据端点”通过WS-MetadataExchange帮我们实现了对服务的描述,提供了WSDL,启动Host之后我们可以通过<http://localhost:8732/Design_Time_Addresses/WcfServiceLib/Service1/?wsdl> 查看到公开的服务描述。
配置节展开如下图:

关于WCF中的地址和绑定,需要补充一下。
WCF中支持的传输协议包括HTTP、TCP、Peer network(对等网)、IPC(基于命名管道的内部进程通信)以及MSMQ(微软消息队列),每个协议对应一个地址类型:
- HTTP地址:<http://localhost:8080/>
- TCP地址: net.tcp://localhost:8080/
- IPC地址: net.pipe://localhost/ (适用于跨进程,不能使用于不同机器间)
- MSMQ地址: net.msmq://localhost/
- 对等网地址: net.p2p://localhost/
WCF中提供的绑定有:
- BasicHttpBinding: 最简单的绑定类型,通常用于 Web Services。使用 HTTP 协议,Text/XML 编码方式。
- WSHttpBinding: 比 BasicHttpBinding 更加安全,通常用于 non-duplex 服务通讯。
- WSDualHttpBinding: 和 WSHttpBinding 相比,它支持 duplex 类型的服务。
- WSFederationHttpBinding: 支持 WS-Federation 安全通讯协议。
- NetTcpBinding: 效率最高,安全的跨机器通讯方式。
- NetNamedPipeBinding: 安全、可靠、高效的单机服务通讯方式。
- NetMsmqBinding: 使用消息队列在不同机器间进行通讯。
- NetPeerTcpBinding: 使用 P2P 协议在多机器间通讯。
- MsmqIntegrationBinding: 使用现有的消息队列系统进行跨机器通讯。如 MSMQ。
------ 弱弱的分隔线 -----

OK,有了上面的基础,就让WCF风暴来的猛烈些吧。做一个多服务,多端点的示例。
1.WcfServiceLib 代码:
[ServiceContract]
publicinterfaceIService
{
[OperationContract]
stringGetMessage();
}
publicclassService1 : IService
{
publicstringGetMessage()
{
var address = OperationContext.Current.Channel.LocalAddress.ToString();
returnstring.Format("From Server1: Hello Client at [{0}]", address);
}
}
publicclassService2 : IService
{
publicstringGetMessage()
{
var address = OperationContext.Current.Channel.LocalAddress.ToString();
returnstring.Format("来自Service2: 好Client at [{0}]", address);
}
}
2.WcfConsoleHost 代码:
staticvoidMain(string[] args)
{
ServiceHost host1 = newServiceHost(typeof(WcfServiceLib.Service1));
host1.Open();
Console.WriteLine("Server1 Opened!");
ServiceHost host2 = newServiceHost(typeof(WcfServiceLib.Service2));
host2.Open();
Console.WriteLine("Server2 Opened!");
Console.Read();
}
3.服务端配置文件:
<?xmlversion="1.0"encoding="utf-8"?>
<configuration>
<system.web>
<compilationdebug="true"/>
</system.web>
<system.serviceModel>
<services>
<servicename="WcfServiceLib.Service1">
<host>
<baseAddresses>
<addbaseAddress=
"http://localhost:9999/WcfStudy3/Service1"/>
<addbaseAddress=
"net.tcp://localhost:8888/WcfStudy3/Service1"/>
</baseAddresses>
</host>
<endpointaddress="serviceEN_1"binding="wsHttpBinding"contract="WcfServiceLib.IService"/>
<endpointaddress="serviceEN_2"binding="mexTcpBinding"contract="WcfServiceLib.IService"/>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
<servicename="WcfServiceLib.Service2">
<host>
<baseAddresses>
<addbaseAddress=
"http://localhost:9999/WcfStudy3/Service2"/>
<addbaseAddress=
"net.tcp://localhost:8888/WcfStudy3/Service2"/>
</baseAddresses>
</host>
<endpointaddress="serviceCH_1"binding="wsHttpBinding"contract="WcfServiceLib.IService"/>
<endpointaddress="serviceCH_2"binding="mexTcpBinding"contract="WcfServiceLib.IService"/>
<endpointaddress="mex"binding="mexHttpBinding"contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadatahttpGetEnabled="True"/>
<serviceDebugincludeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
4. 启动Host,在Client工程中添加Service Reference
因为有两个Service,所以要添加两次。
(1) WcfSvc1(Url:http://localhost:9999/WcfStudy3/Service1)
(2) WcfSvc2(Url:<http://localhost:9999/WcfStudy3/Service2>) 图略
5. 客户端配置文件: 配置节中,生成了4个Endpoint,分别对应服务端的4个Endpoint。通过
name属性区别。
<client>
<endpoint
address="http://localhost:9999/WcfStudy3/Service1/serviceEN_1"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService"
contract="WcfSvc1.IService"
name="WSHttpBinding_IService">
</endpoint>
<endpoint
address="net.tcp://localhost:8888/WcfStudy3/Service1/serviceEN_2"
binding="netTcpBinding"
bindingConfiguration="MetadataExchangeTcpBinding_IService"
contract="WcfSvc1.IService"
name="MetadataExchangeTcpBinding_IService"
/>
<endpoint
address="http://localhost:9999/WcfStudy3/Service2/serviceCH_1"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService1"
contract="WcfSvc2.IService"
name="WSHttpBinding_IService1">
</endpoint>
<endpoint
address="net.tcp://localhost:8888/WcfStudy3/Service2/serviceCH_2"
binding="netTcpBinding"
bindingConfiguration="MetadataExchangeTcpBinding_IService1"
contract="WcfSvc2.IService"
name="MetadataExchangeTcpBinding_IService1"
/>
</client>
6.
客户端代码:
staticvoidMain(string[] args)
{
Console.WriteLine("------------");
WcfSvc1.ServiceClient
client1_1 = newWcfSvc1.ServiceClient("WSHttpBinding_IService");
Console.WriteLine(client1_1.GetMessage());
Console.WriteLine("------------");
WcfSvc1.ServiceClient
client1_2 = newWcfSvc1.ServiceClient("MetadataExchangeTcpBinding_IService");
Console.WriteLine(client1_2.GetMessage());
Console.WriteLine("------------");
WcfSvc2.ServiceClient
client2_1 = newWcfSvc2.ServiceClient("WSHttpBinding_IService1");
Console.WriteLine(client2_1.GetMessage());
Console.WriteLine("------------");
WcfSvc2.ServiceClient
client2_2 = newWcfSvc2.ServiceClient("MetadataExchangeTcpBinding_IService1");
Console.WriteLine(client2_2.GetMessage());
Console.Read();
}
7.运行结果:
有人会问,那么生成完的配置文件都要一个个手动修改吗?答案当然不是,VS已经为我们准备了WCF配置工具:IDE > Tools > WCF
Service Configuration Editor 。 关于工具的使用,大家可以看这里: <http://www.rainsts.net/article.asp?id=441>
WCF配置详解的更多相关文章
- Log4j配置详解(转)
一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...
- logback 常用配置详解<appender>
logback 常用配置详解 <appender> <appender>: <appender>是<configuration>的子节点,是负责写日志的 ...
- [转]阿里巴巴数据库连接池 druid配置详解
一.背景 java程序很大一部分要操作数据库,为了提高性能操作数据库的时候,又不得不使用数据库连接池.数据库连接池有很多选择,c3p.dhcp.proxool等,druid作为一名后起之秀,凭借其出色 ...
- libCURL开源库在VS2010环境下编译安装,配置详解
libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...
- logback配置详解3<filter>
logback 常用配置详解(三) <filter> <filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY ...
- logback配置详解2<appender>
logback 常用配置详解(二) <appender> <appender>: <appender>是<configuration>的子节点,是负责写 ...
- log4j.properties配置详解
1.Loggers Loggers组件在此系统中被分为五个级别:DEBUG.INFO.WARN.ERROR和FATAL.这五个级别是有顺序的,DEBUG < INFO < WARN < ...
- Log4J日志配置详解
一.Log4j简介 Log4j有三个主要的组件:Loggers(记录器),Appenders (输出源)和Layouts(布局).这里可简单理解为日志类别,日志要输出的地方和日志以何种形式输出.综合使 ...
- Windows下Nginx Virtual Host多站点配置详解
Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 window ...
随机推荐
- ReentrantLock的原理学习
转载:https://my.oschina.net/andylucc/blog/651982 摘要 提到JAVA加锁,我们通常会想到synchronized关键字或者是Java Concurrent ...
- java配置自动任务,定期执行代码
任务调用类: package business.tools.service; import java.util.ArrayList; import java.util.Calendar; import ...
- hnu Dirichlet's Theorem
/* 求ax+b x属于区间[L,R];范围内素数的个数. a*R+b<=10^12 ; R-L+1<=10^6 枚举,超时. 1.如果GCD(a,b)>1 那么a+b 2*a+b ...
- 编写一个JAVA小程序取得IP地址
在TCP/IP 互联网时,经常会需要查询自己主机的IP地址和www服务器的IP地址.虽然,我们可以使用IPCONFIG 和PING 进行IP地址查询,但是如果在应用程序或APPLET中使用此命令会破坏 ...
- Keytool生成csr
一. 首先生成密钥库 keytool -genkey -keyalg RSA -keysize 4096 -keystore c:\keystore4096.jks 二.生成csr keytool - ...
- shell中awk用法
简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...
- Linux常用性能检测命令解释
1.uptime [root@smgsim02 ~]# uptime 15:08:15 up 98 days, 4:19, 2 users, load average: 0.07, 0.29, 0.1 ...
- flume ng系列之——flume安装
flume版本:1.5.0 1.下载安装包: http://www.apache.org/dyn/closer.cgi/flume/1.5.0/apache-flume-1.5.0-bin.tar.g ...
- 关于Android6.0之后的权限问题
https://github.com/mylhyl/AndroidAcp AndroidAcp 使用: 加入 compile 'com.mylhyl:acp:1.1.7' PermisionUtils ...
- VC++检测当前网络状态
VC获得本机网络连接状态 转载:http://www.cppblog.com/wrhwww/archive/2010/12/02/135230.html //本机网络连接类型(成功) #define ...