WCF分布式开发步步为赢(3)WCF服务元数据交换、配置及编程开发
【3】WCF服务元数据交换配置实现过程详解:
【3.1】配置HTTP-GET元数据交换方式:
需要配置服务的行为和基地址,客户端可以根据基地址查看服务的元数据。代码如下:
<service name="WcfServiceApp.WCFService" behaviorConfiguration="WcfServiceApp.WCFServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
baseAddresses>
host>
service>
services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceApp.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
behavior>
serviceBehaviors>
behaviors>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
baseAddresses>
host>
service>
services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfServiceApp.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
behavior>
serviceBehaviors>
behaviors>
配置完成以后,我们可以使用基地址在浏览器里查看服务的信息,效果如下图:
【3.2】配置终结点元数据交换方式:
我们这里配置了3种方式的元数据交换终结点,分别是HTTP、TCP、IPC方式。具体代码如下:
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
<add baseAddress="net.tcp://localhost:8002/"/>
<add baseAddress="net.pipe://localhost/"/>
</baseAddresses>
</host>
服务行为:
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
behavior>
serviceBehaviors>
behaviors>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<endpoint address="mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
<add baseAddress="net.tcp://localhost:8002/"/>
<add baseAddress="net.pipe://localhost/"/>
</baseAddresses>
</host>
服务行为:
<serviceBehaviors>
<behavior name="WCFService.WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
behavior>
serviceBehaviors>
behaviors>