WCF配置详解

WCF配置详解

服务端的配置文件主要是对services、bindings、behaviors的配置。在默认的App.config中,使用的是WCF Framework定义好的wsHttpBinding默认配置,所以看不到binding配置节。

配置节展开如下图:

BTW: "元数据端点”通过WS-MetadataExchange帮我们实现了对服务的描述,提供了WSDL,启动Host之后我们可以通过 查看到公开的服务描述。

配置节展开如下图:

关于WCF中的地址和绑定,需要补充一下。

WCF中支持的传输协议包括HTTP、TCP、Peer network(对等网)、IPC(基于命名管道的内部进程通信)以及MSMQ(微软消息队列),每个协议对应一个地址类型:

HTTP地址:

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.服务端配置文件:

"http://localhost:9999/WcfStudy3/Service1"/>

"net.tcp://localhost:8888/WcfStudy3/Service1"/>

"http://localhost:9999/WcfStudy3/Service2"/>

"net.tcp://localhost:8888/WcfStudy3/Service2"/>

4. 启动Host,在Client工程中添加Service Reference

因为有两个Service,所以要添加两次。 (1) WcfSvc1(Url:http://localhost:9999/WcfStudy3/Service1)

(2) WcfSvc2(Url:) 图略

5. 客户端配置文件: 配置节中,生成了4个Endpoint,分别对应服务端的4个Endpoint。通过 name属性区别。

address="http://localhost:9999/WcfStudy3/Service1/serviceEN_1"

binding="wsHttpBinding"

bindingConfiguration="WSHttpBinding_IService"

contract="WcfSvc1.IService"

name="WSHttpBinding_IService">

address="net.tcp://localhost:8888/WcfStudy3/Service1/serviceEN_2"

binding="netTcpBinding"

bindingConfiguration="MetadataExchangeTcpBinding_IService"

contract="WcfSvc1.IService"

name="MetadataExchangeTcpBinding_IService"

/>

address="http://localhost:9999/WcfStudy3/Service2/serviceCH_1"

binding="wsHttpBinding"

bindingConfiguration="WSHttpBinding_IService1"

contract="WcfSvc2.IService"

name="WSHttpBinding_IService1">

address="net.tcp://localhost:8888/WcfStudy3/Service2/serviceCH_2"

binding="netTcpBinding"

bindingConfiguration="MetadataExchangeTcpBinding_IService1"

contract="WcfSvc2.IService"

name="MetadataExchangeTcpBinding_IService1"

/>

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 。

相关文章

红楼梦(87版)
microsoft365破解版

红楼梦(87版)

🕒 07-20 👁️ 6559
华硕灵耀3 deluxe散热好不好
microsoft365破解版

华硕灵耀3 deluxe散热好不好

🕒 06-28 👁️ 4460
願(愿)
microsoft365破解版

願(愿)

🕒 07-01 👁️ 1301
学习时报专版报道:建好用好政协协商平台 推进协商民主广泛多层制度化发展
抖音拍同款特效不可用怎么办 抖音拍同款特效的解决方法
红米Note2手机怎么样?红米Note 2详细评测图解
microsoft365破解版

红米Note2手机怎么样?红米Note 2详细评测图解

🕒 08-01 👁️ 9553
狗狗咬人怎么办?三个小妙招帮助纠正(了解咬人的原因,掌握三个有效方法)
2025年海鲜水产行业现状与发展趋势分析
microsoft365破解版

2025年海鲜水产行业现状与发展趋势分析

🕒 07-28 👁️ 2257
配音秀怎么下载自己喜欢的的作品 配音秀中下载的作品在哪找