上篇文章主要讨论了WCF的基本内容,其中包括WCF的术语、创建方法及WCF在开发过程中使用的意义,它不仅能够提供程序之间的通信,而且还能提供程序和数据间的通信,WCF提供了多样化的程序之间的通信,不仅支持App的通信而且还支持web与应用程序之间的通信,可谓是功能强大。虽然上文讨论了WCF的基本使用方法,并做了很多Demo,但是都只是关于WCF的创建和调用的,今天来看看WCF的配置方法。
WCF 配置导图



上图整理了服务配置过程中所用到的基本的元素,大致的步骤主要是首先要在调用服务的程序集中添加服务的一个引用,然后添加一个service并指定服务的名称、终结点,如果添加了behavior(行为)的配置,那么也需要添加一个behaviorConfiguration的配置信息。在添加一个service时会在其中指定终结点的信息,终结点说的就是服务的具体信息访问方式,在终结点中添加服务address及binding信息和contract(服务契约)信息等。在endpoint中添加的binding只是指定了服务绑定的访问方式,例如:basicHttpBinding等,真正的binding配置是需要在另外的binding中添加的,添加好后需要配置在endpoint的bindingConfiguration。下面看一个服务在客户端部署配置的示例。

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <startup>
  4. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  5. </startup>
  6. <system.serviceModel>
  7. <!--The bindings configuration
  8. We deployed the basichttpbinding.
  9. I added a binding that name is binding1.
  10. The binding1 used the security.
  11. -->
  12. <bindings>
  13. <basicHttpBinding>
  14. <binding name="binding1">
  15. <security mode="Message">
  16. <message clientCredentialType="Certificate"/>
  17. </security>
  18. </binding>
  19. </basicHttpBinding>
  20. </bindings>
  21. <!--The behaviors configuration
  22. I added a serviceBehavior that has nothing configuration in.
  23. -->
  24. <behaviors>
  25. <serviceBehaviors>
  26. <behavior name="service1_behavior">
  27. </behavior>
  28. </serviceBehaviors>
  29. </behaviors>
  30. <!--The services configuration
  31. I added a service that behaviorConfiguration is service1_behavior.
  32. This service has an endpoint that added the property and the binding.
  33. -->
  34. <services>
  35. <service name="Wcfservice.Service1" behaviorConfiguration="service1_behavior">
  36. <endpoint address="http://localhost:64921/Service1.svc" name="ServiceReference1_IService1" binding="basicHttpBinding" bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
  37. </service>
  38. </services>
  39. </system.serviceModel>
  40. </configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel> <!--The bindings configuration
We deployed the basichttpbinding.
I added a binding that name is binding1.
The binding1 used the security.
-->
<bindings>
<basicHttpBinding>
<binding name="binding1">
<security mode="Message">
<message clientCredentialType="Certificate"/>
</security>
</binding>
</basicHttpBinding>
</bindings> <!--The behaviors configuration
I added a serviceBehavior that has nothing configuration in.
-->
<behaviors>
<serviceBehaviors>
<behavior name="service1_behavior"> </behavior>
</serviceBehaviors>
</behaviors> <!--The services configuration
I added a service that behaviorConfiguration is service1_behavior.
This service has an endpoint that added the property and the binding.
-->
<services>
<service name="Wcfservice.Service1" behaviorConfiguration="service1_behavior">
<endpoint address="http://localhost:64921/Service1.svc" name="ServiceReference1_IService1" binding="basicHttpBinding" bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
</service>
</services> </system.serviceModel>
</configuration>

上例将配置信息写入到了xml中,分别添加了一个服务的binding、service和behavior,在添加时往往是自下向上添加,首先添加一个binding配置信息,然后添加相应的behavior行为,最后添加一个service并把绑定信息添加到其中。

一、service

service主要是用来配置endpoint和host的,它实际上代表了为程序集添加了一个服务的引用,其中的endpoint指定了服务的地址、绑定和协议,host则提供了服务寄宿的方式。



    如下配置:

  1. <services>
  2. <service name="Wcfservice.Service1">
  3. <host>
  4. <baseAddresses>
  5. <add baseAddress="http://localhost:64921/ConsoleApplication1"/>
  6. </baseAddresses>
  7. </host>
  8. <endpoint address="http://localhost:64921/WcfService1.IService1" binding="basicHttpBinding" bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
  9. </service>
  10. </services>
<services>
<service name="Wcfservice.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:64921/ConsoleApplication1"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:64921/WcfService1.IService1" binding="basicHttpBinding" bindingConfiguration="binding1" contract="ServiceReference1.IService1"></endpoint>
</service>
</services>

在上面的代码中添加了一个service,并为service配置了一个host该host的应用程序即为当前引用服务的程序集。最后添加了一个endpoint,终结点中绑定了URI,URI的地址就是WCF的定义地址,其中的binding是指定了基本的绑定类型,另外还使用contract指定了服务契约的接口。

二、binding

service是对服务进行的配置,指定了服务的一些配置信息,另外很重要的和service同级还有binding,它是对消息访问方式做的一些配置。

1、绑定方式

分为系统自带的绑定和用户自定义绑定两种,系统自带的绑定包括basicHttpBinding、WcHttpBinding等。

系统默认绑定方式

如果系统提供的绑定功能不完全,那么也可以使用用户自定义的绑定功能,可以使用customBinding对象从预先存在的绑定元素中创建新的绑定,也可以通过从Binding派生类来创建完全由用户自定义的绑定。

2、基本功能

绑定除了定义绑定的方式外,还可以指定传输协议的类型、安全性、编码方式和事务等,通过绑定来配置WCF的基本操作类型,这样能够对服务做详细的一些配置,使服务的功能更加健全。

  1. <?xml version=”1.0” encoding=”utf-8” ?>
  2. <configuration>
  3. <system.serviceModel>
  4. <services>
  5. <service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
  6. <host>
  7. <baseAddresses>
  8. <add baseAddress=”net.pipe://localhost/WCFService”/>
  9. <add baseAddress=”net.tcp://localhost:8000/WCFService”/>
  10. <add baseAddress=”http://localhost:8080/WCFService”/>
  11. </baseAddresses>
  12. </host>
  13. <endpoint address=”tcpmex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
  14. <endpoint address=”namedpipemex” binding=”mexNamedPipeBinding” contract=”IMetadataExchange”/>
  15. <endpoint address=”” binding=”wsHttpBinding” contract=”WCFService.IServiceClass”/>
  16. </service>
  17. </services>
  18. <behaviors>
  19. <serviceBehaviors>
  20. <behavior name=”metadataSupport”>
  21. <serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
  22. </behavior>
  23. </serviceBehaviors>
  24. </behaviors>
  25. </system.serviceModel>
  26. </configuration>
<?xml version=”1.0” encoding=”utf-8” ?>
<configuration>
<system.serviceModel>
<services>
<service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
<host>
<baseAddresses>
<add baseAddress=”net.pipe://localhost/WCFService”/>
<add baseAddress=”net.tcp://localhost:8000/WCFService”/>
<add baseAddress=”http://localhost:8080/WCFService”/>
</baseAddresses>
</host>
<endpoint address=”tcpmex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
<endpoint address=”namedpipemex” binding=”mexNamedPipeBinding” contract=”IMetadataExchange”/>
<endpoint address=”” binding=”wsHttpBinding” contract=”WCFService.IServiceClass”/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”metadataSupport”>
<serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

三、Behavior

行为属性,行为属性可以控制服务的运行时特性,主要分为服务行为和操作行为,这些行为或特性,可以通过配置runtime属性配置文件,或自定义行为来实现。

  1. <?xml version=”1.0” encoding=”utf-8” ?>
  2. <configuration>
  3. <system.serviceModel>
  4. <services>
  5. <service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
  6. <host>
  7. <baseAddresses>
  8. <add baseAddress=”net.pipe://localhost/WCFService”/>
  9. <add baseAddress=”net.tcp://localhost:8000/WCFService”/>
  10. <add baseAddress=”http://localhost:8080/WCFService”/>
  11. </baseAddresses>
  12. </host>
  13. <endpoint address=”tcpmex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
  14. <endpoint address=”namedpipemex” binding=”mexNamedPipeBinding” contract=”IMetadataExchange”/>
  15. <endpoint address=”” binding=”wsDualHttpBinding” contract=”WCFService.IServiceClass”/>
  16. <!--<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”/>-->
  17. </service>
  18. </services>
  19. <behaviors>
  20. <serviceBehaviors>
  21. <behavior name=”metadataSupport”>
  22. <serviceDebug includeExceptionDetailInFaults=”true”/>
  23. <serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
  24. <serviceThrottling maxConcurrentCalls=”10” maxConcurrentInstances=”5” maxConcurrentSessions=”5”/>
  25. <serviceSecurityAudit auditLogLocation=”Application” suppressAuditFailure=”false”/>
  26. </behavior>
  27. </serviceBehaviors>
  28. </behaviors>
  29. </system.serviceModel>
  30. </configuration>
<?xml version=”1.0” encoding=”utf-8” ?>
<configuration>
<system.serviceModel>
<services>
<service name =”WCFService.ServiceClass” behaviorConfiguration=”metadataSupport”>
<host>
<baseAddresses>
<add baseAddress=”net.pipe://localhost/WCFService”/>
<add baseAddress=”net.tcp://localhost:8000/WCFService”/>
<add baseAddress=”http://localhost:8080/WCFService”/>
</baseAddresses>
</host>
<endpoint address=”tcpmex” binding=”mexTcpBinding” contract=”IMetadataExchange”/>
<endpoint address=”namedpipemex” binding=”mexNamedPipeBinding” contract=”IMetadataExchange”/>
<endpoint address=”” binding=”wsDualHttpBinding” contract=”WCFService.IServiceClass”/>
<!--<endpoint address=”mex” binding=”mexHttpBinding” contract=”IMetadataExchange”/>-->
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name=”metadataSupport”>
<serviceDebug includeExceptionDetailInFaults=”true”/>
<serviceMetadata httpGetEnabled=”false” httpGetUrl=””/>
<serviceThrottling maxConcurrentCalls=”10” maxConcurrentInstances=”5” maxConcurrentSessions=”5”/>
<serviceSecurityAudit auditLogLocation=”Application” suppressAuditFailure=”false”/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Note:serviceMetadata 是一种元数据启用功能,它是配置元数据终结点,默认情况下是不公开元数据的,但是可以通过启用配置来公开元数据的终结点。

       上面的代码都是使用的是配置文件做的服务的配置部署,另外也可在程序中编写代码来配置部署信息,但是并不赞成这种配置方式,因为这种配置方式不易更改,当你部署到客户环境后就不能再更改内部的代码结构,所以这种方式很不灵活,并不提倡使用这种方式来配置服务,但是可以作为了解,如下代码:

  1. namespace ConsoleApplication1
  2. {
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. using (ServiceHost host=new ServiceHost(typeof(ServiceReference1.Service1Client)))
  8. {
  9. host.AddServiceEndpoint(typeof(ServiceReference1.IService1), new WSHttpBinding(), "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1");
  10. if (host.Description.Behaviors.Find<ServiceMetadataBehavior>()==null)
  11. {
  12. ServiceMetadataBehavior serviceMetadata=new ServiceMetadataBehavior();
  13. serviceMetadata.HttpGetEnabled = true;
  14. serviceMetadata.HttpGetUrl = new Uri("http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/metadata");
  15. host.Description.Behaviors.Add(serviceMetadata);
  16. }
  17. host.Open();
  18. ServiceReference1.IService1 service1 = new ServiceReference1.Service1Client();
  19. service1.GetData(1);
  20. Console.Write("fdsf");
  21. host.Close();
  22. }
  23. }
  24. }
  25. }
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host=new ServiceHost(typeof(ServiceReference1.Service1Client)))
{
host.AddServiceEndpoint(typeof(ServiceReference1.IService1), new WSHttpBinding(), "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1");
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>()==null)
{
ServiceMetadataBehavior serviceMetadata=new ServiceMetadataBehavior();
serviceMetadata.HttpGetEnabled = true;
serviceMetadata.HttpGetUrl = new Uri("http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/metadata");
host.Description.Behaviors.Add(serviceMetadata);
} host.Open(); ServiceReference1.IService1 service1 = new ServiceReference1.Service1Client();
service1.GetData(1);
Console.Write("fdsf");
host.Close();
}
}
}
}

上面的代码中使用的是WSHttpBinding方式它支持双工通信,另外上面的宿主方式使用的是ConsoleApplication
Host,这种host方式非常简单。在创建宿主程序时,需要为宿主指定宿主服务的类型,这里的类型要使用实现服务的类,最好不要使用接口类型。

创建宿主对象后,接下来为宿主添加了一个服务的终结点,终结点中第一个参数是指定了协议实现的类型,本例使用的是接口协议,所以要配置为相应的接口类型;第二个参数指定了绑定的类型;第三个参数则指定了终结点的URI地址,URI地址要配置服务具体实现的类URL地址。

接下来为宿主添加了一个行为(Behavior),并为行为公开了元数据,这种行为在创建时也可以不强加给服务,也就是说在添加宿主时,服务的行为定义是可选的,也可以不定义。

结语

本文主要针对WCF在客户端进行配置时所使用的基本的配置节做了详细的讨论,主要是Service、Binding和Behavior的应用,另外需要注意的是在客户端进行服务配置时不建议采用代码配置的方法,最好使用xml文件进行发布配置,这样能很好的修改。最后还有在添加WCF时一定要根据不同的类别添加需要的WCF,WCF中有类库和Application两种,它们所针对的Host是不同的,如果是类库的话往往用来配置Console
Application作为Host,如果是Application类的往往用来配置网站服务,否则在使用时很容易出错。

版权声明:本文为博主原创文章,未经博主允许不得转载。

【WCF全析(二)】--服务配置部署详解的更多相关文章

  1. Solr部署详解

    Solr部署详解 时间:2013-11-24 方式:转载 目录 1 solr概述 1.1 solr的简介 1.2 solr的特点 2 Solr安装 2.1 安装JDK 2.2 安装Tomcat 2.3 ...

  2. 【转】Nginx+php-fpm+MySQL分离部署详解

    转:http://www.linuxidc.com/Linux/2015-07/120580.htm Nginx+php-fpm+MySQL分离部署详解 [日期:2015-07-26] 来源:Linu ...

  3. MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解

    MySQL高可用方案-PXC(Percona XtraDB Cluster)环境部署详解 Percona XtraDB Cluster简称PXC.Percona Xtradb Cluster的实现是在 ...

  4. 【第六课】Nginx常用配置下详解

    目录 Nginx常用配置下详解 1.Nginx虚拟主机 2.部署wordpress开源博客 3.部署discuz开源论坛 4.域名重定向 5.Nginx用户认证 6.Nginx访问日志配置 7.Ngi ...

  5. CentOS7下Firewall防火墙配置用法详解

    官方文档地址: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide ...

  6. (原创)LAMP搭建之二:apache配置文件详解(中英文对照版)

    LAMP搭建之二:apache配置文件详解(中英文对照版) # This is the main Apache server configuration file. It contains the # ...

  7. Velocity魔法堂系列二:VTL语法详解

    一.前言 Velocity作为历史悠久的模板引擎不单单可以替代JSP作为Java Web的服务端网页模板引擎,而且可以作为普通文本的模板引擎来增强服务端程序文本处理能力.而且Velocity被移植到不 ...

  8. Dubbo配置方式详解

    Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是阿里巴巴 SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次 ...

  9. Java编程配置思路详解

    Java编程配置思路详解 SpringBoot虽然提供了很多优秀的starter帮助我们快速开发,可实际生产环境的特殊性,我们依然需要对默认整合配置做自定义操作,提高程序的可控性,虽然你配的不一定比官 ...

随机推荐

  1. requests 快速入门

     requests的请求方式 import requests # 发送请求 r = requests.get('https://github.com/timeline.json') r = reque ...

  2. D3树状图给指定特性的边特别显示颜色

    D3作为前端图形显示的利器,功能之强,对底层技术细节要求相对比较多. 有一点,就是要理解其基本的数据和节点的匹配规则架构,即enter,update和exit原理,我前面的D3基础篇中有介绍过,不明白 ...

  3. 利用百度云免费备份SQL数据库

    我们开发了一个会员管理系统,随着使用的人越来越多,异地备份数据库就显得十分重要,万一硬盘出问题了怎么办呢.所以就着手做这个工作. 首先呢,找到了几个专门用来提供备份数据库的网站,一年好几百,好贵.放弃 ...

  4. SQL2008中Merge的用法

    在SQL2008中,新增了一个关键字:Merge,这个和Oracle的Merge的用法差不多,只是新增了一个delete方法而已.下面就是具体的使用说明: 首先是对merge的使用说明: merge ...

  5. windows 自带的 端口映射 端口转向功能

    安装IPV6 netsh interface ipv6 install查看 netsh interface portproxy show all添加 netsh interface portproxy ...

  6. ios 写项目的时候遇到的问题及解决方案(2)

    11.自适应文本高度 NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:]}; CGRect rec ...

  7. Rails : css或js文件无法成功预编译或调用jquery类插件时预编译问题

    调用bootstrap css框架时,将bootstrap文件夹放入 vendor/assets/下 bootstrap文件结构如下:    [shenma@localhost demo]$ ls v ...

  8. [综]前景检测GMM

    tornadomeet 前景检测算法_4(opencv自带GMM) http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531705.html ...

  9. WeX5 - AJAX跨域调用相关知识-CORS和JSONP

    http://docs.wex5.com/ajax-cross-domain/ 1.什么是跨域 跨域问题产生的原因,是由于浏览器的安全机制,JS只能访问与所在页面同一个域(相同协议.域名.端口)的内容 ...

  10. python---dnspython

    dnspython 是Python实现的一个DNS工具包,支持几乎所有的记录类型,可以用于查询,传输并动态更新ZONE信息,同时支持TSIG(事务签名)验证消息和EDNS0(扩展DNS).可以替代ns ...