| WCF(三) Message pattern 上次咱们稍稍讨论了WCF中的contract,这次咱们讨论Message 
                          Pattern. WCF中为何会有message pattern呢? 我们知道WCF的Server端与client端是通过xml 来进行交互的.message pattern其实就是指server端与client端交互xml的方式. wcf提供以下三种pattern: 1.Request-reply:client端调用server端的公开的method后,client端需等server端method返回结果(即:reply响应)以后,才能做继续client端的下一步操作.(如:client调用server端的method往数据库中插入一条数据.server端插入数据成功以后,返回相应的数据库插入成功信息.很多情况下使用的是这种. 2.one-way:client端调用server端的公开的method后,client端不需等server端的method返回结果,便可继续client端下一步操作.(如:server端记录client端的日志(log)信息).标识为One-way的operation(函数)的返回值只能是void. 
                          当client向server 发起了request后,不需要server做出回应的情况使用one-way 
                          message pattern. 3.duplicate:Request-Reply 和One-way都是先由client端发起的request(请求);duplicate则允许有server端发起request. 两种方式实现duplicate:一种是tcp(这里先不讲tcp的方式,先讲http的方式).一种是http. 
                          Http方式的话:是先由client端发起一次one-way 的request,然后client不等待server端返回结果,而是端继续往下执行.当server端有结果返回时,再有server端发起一次one-way的request.所以duplicate也叫callback. 
                          我们知道当一个操作很费时的话,为了减少UI的无响应,有更好的用户体验,会使用callback.wcf也是:当server的执行很耗时的话,我们可以使用duplicate的message 
                          pattern. 4.实现: 4.1:Requst-Reply:是最简单的 
                           
                            | [ServiceContract(CallbackContract = typeof(IDuplicateDemoCallback))]2     public interface IService1
 3     {
 4         // 在函数中,我们让线程暂停,模拟耗时的操作.
 5         // 参数:interval为暂停的毫秒数,由调用该method的client端设定.
 6         [OperationContract]
 7         void RequestReply(int interval);
 8
 9         [OperationContract]
 10         void DuplicateCommunicate(int interval);
 11
 12     }
 |  4.2:One-Way:(只需要在requst-reply的基础上,使operationContract 
                          的IsOneWay=true即可) 
                           
                            | [ServiceContract(CallbackContract = typeof(IDuplicateDemoCallback))]2     public interface IService1
 3     {
 4         // 在函数中,我们让线程暂停,模拟耗时的操作.
 5         // 参数:interval为暂停的毫秒数,由调用该method的client端设定.
 6         [OperationContract(IsOneWay = true)]
 7         void RequestReply(int interval);
 8
 9         [OperationContract(IsOneWay = true)]
 10         void DuplicateCommunicate(int interval);
 11
 12     }
 |  4.3Duplicate:(有些复杂)有以下步骤: 4.3.1:在server端IService.cs文件中添加代码: 
                           
                            | // NOTE: You can use the "Rename" command on the "Refactor" menu to 
change the interface name "IService1" in both code and config file together.2     [ServiceContract(CallbackContract = typeof(IDuplicateDemoCallback))]
 3     public interface IService1
 4     {
 5         // 在函数中,我们让线程暂停,模拟耗时的操作.
 6         // 参数:interval为暂停的毫秒数,由调用该method的client端设定.
 7         [OperationContract(IsOneWay=true)]
 8         void RequestReply(int interval);
 9
 10         [OperationContract(IsOneWay=true)]
 11         void DuplicateCommunicate(int interval);
 12
 13
 14     }
 15
 16     public interface IDuplicateDemoCallback
 17     {
 18         [OperationContract(IsOneWay=true)]
 19         void DuplicateCommunicateCallback(string callbackMessage);
 20     }
 |  4.3.2:在web site项目下的web.config文件:使其支持wsDua 
                           
                            | <?xml version="1.0"?>2 <configuration>
 3
 4   <system.web>
 5     <compilation debug="false" targetFramework="4.0" />
 6   </system.web>
 7   <system.serviceModel>
 8     <behaviors>
 9       <serviceBehaviors>
 10         <behavior>
 11           <!-- To avoid disclosing metadata information, 
set the value below to false and remove the metadata endpoint above before deployment -->
 12           <serviceMetadata httpGetEnabled="true"/>
 13           <!-- To receive exception details in faults for debugging purposes,
 set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
 14           <serviceDebug includeExceptionDetailInFaults="false"/>
 15         </behavior>
 16       </serviceBehaviors>
 17     </behaviors>
 18     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
 19
 20     <services>
 21       <service name="WcfMessagePatternLib.Service1" behaviorConfiguration="">
 22         <!-- Service Endpoints -->
 23         <endpoint address="" binding="wsDualHttpBinding" contract="WcfMessagePatternLib.IService1">
 24           <identity>
 25             <dns value="localhost"/>
 26           </identity>
 27         </endpoint>
 28         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
 29       </service>
 30     </services>
 31
 32
 33     </system.serviceModel>
 34   <system.webServer>
 35     <modules runAllManagedModulesForAllRequests="true"/>
 36   </system.webServer>
 37
 38 </configuration>
 |  4.3.3在client端添加一个类文件命名为Callback.cs: 使其实现IDuplicateDemoCallback接口. 
                           
                            | class Callback:ServiceReference1.IService1Callback2     {
 3         public void DuplicateCommunicateCallback(string callbackMessage)
 4         {
 5             MessageBox.Show(callbackMessage);
 6         }
 7     }
 复制代码
 1         private void Form1_Load(object sender, EventArgs e)
 2         {
 3             var context = new InstanceContext(new Callback());
 4             proxy = new Service1Client(context);
 5         }
 |   4.3.4修改client项目下的app.config文件:使其支持wsdualhttp 
                           
                            | <?xml version="1.0" encoding="utf-8" ?>2 <configuration>
 3     <system.serviceModel>
 4         <bindings>
 5             <wsDualHttpBinding>
 6                 <binding name="WSDualHttpBinding_IService1" closeTimeout="00:01:00"
 7                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
 8                     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
 9                     maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
 10                     messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
 11                     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
 12                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
 13                     <reliableSession ordered="true" inactivityTimeout="00:10:00" />
 14                     <security mode="Message">
 15                         <message clientCredentialType="Windows" negotiateServiceCredential="true"
 16                             algorithmSuite="Default" />
 17                     </security>
 18                 </binding>
 19             </wsDualHttpBinding>
 20         </bindings>
 21         <client>
 22             <endpoint address="http://localhost:8167/WebHost/Service.svc"
 23                 binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IService1"
 24                 contract="ServiceReference1.IService1" name="WSDualHttpBinding_IService1">
 25                 <identity>
 26                     <dns value="localhost" />
 27                 </identity>
 28             </endpoint>
 29         </client>
 30     </system.serviceModel>
 31 </configuration>
 |  WCF(四) Configuration file (配置文件) 前面大致介绍了点wcf的几个小小的基本概念. 任何一个新技术,都能给我们带来一大堆的基本概念. 
                          当然WCF也不例外. 关于WCF更多的概念,在以后再讨论.我们先讨论如何在不深入了解这一大堆学术定义的前提下,使用这项技术. 在开发过程中,使用configuration file (配置文件)具有很灵活的好处:以后有关于配置的改变,不必重新编译代码,只需打开configuration 
                          file 改一下就行了,省时省力. WCF中configuration file的地位不可忽视.当然我们可以使用code的方式,实现WCF的服务的每个阶段. 
                          但我们的软件产品是需要给客户在实际工作中使用的. 我们要让软件产品的后期维护尽可能的简单,省时省力. WCF的核心是Service. 一个service有包含多个endpoint, 
                          如下图: 
 打开VS2010,File-->New project. 选择WCF Service Library 
                          模板. Solution Name 和project Name我们都用默认的. 点击OK. 此时创建了一个WCF Library project. 打开App.config 文件,你会看到以下内容: 
                           
                            | <?xml version="1.0" encoding="utf-8" ?>2 <configuration>
 3
 4   <system.web>
 5     <compilation debug="true" />
 6   </system.web>
 7   <system.serviceModel>
 8     <services>
 9       <service name="WcfServiceLibrary4.Service1">
 10         <host>
 11           <baseAddresses>
 12             <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary4/Service1/" />
 13           </baseAddresses>
 14         </host>
 15         <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary4.IService1">
 16           <identity>
 17             <dns value="localhost"/>
 18           </identity>
 19         </endpoint>
 20         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
 21       </service>
 22     </services>
 23     <behaviors>
 24       <serviceBehaviors>
 25         <behavior>
 26           <serviceMetadata httpGetEnabled="True"/>
 27           <serviceDebug includeExceptionDetailInFaults="False" />
 28         </behavior>
 29       </serviceBehaviors>
 30     </behaviors>
 31   </system.serviceModel>
 32
 33 </configuration>
 |  注: 第1行,定义该xml file的版本和使用的字符集. 是关于该配置文件的基本信息,与程序配置无关,只管保持默认即可. 第2行,<configuration> node,关于程序内的配置,都必须包含在此node里面. 第5行,是关于debug配置. 如果是在开发阶段,把它设为true,这样调试起来方便. 第7--31行,是<system.serviceModel> node, 关于wcf service的核心配置都在这个node里面. 
                          该node下面有两个子node:一个是<services>,另一个是<behaviors>. 
                          除此以外,还可以有<bindings>等.<services>是<service>的集合;<behaviors>是<behavior>的集合;<bindings>是<binding>的集合. 多说一句: WCF程序设计需要注意两方面的模型:一是编程模型;二是通信模型. 一般我们做web开发只需要编程模型,通信就交给http了. 
                          但是wcf允许我们使用http以外的其他通信协议.所以,我们考虑了编程还必须考虑通信模型.好在wcf给我们提供了方便之处:它屏蔽了开发人员对通信模型的处理,只需要我们通过bindings 
                          API创建通信模型即可. 所以,我们对bindings的指定,就是在处理通信模型. 第8--22行,是<services>node,该node下面有多个<service>node. 第9--21行,是<service>node,它的name指定了实现这个service的类名(全名,包括namespace).该<services>下面有一个<host>node,和多个<endpoint>node.(如上面我们提到的那张图) 
                          . 第10行,是<host>node, <host>下面有<baseAddress>. 第11-13行,<baseAddress> node, <add>增加一个该service的基址,<endpoint>里面的address就相对于这个baseAddress的. 
                          如果有多个baseAddress的话是根据<endpoint>所指定的binding方式与baseAddress的形式匹配的.(如,<endpoint>所指定的binding方式是netTcpBinding,就会匹配以net.tcp://打头的baseAddress.)  第15行,<endpoint>node,<endpoint>除了name以外,还有最重要的三部分组成,简称:ABC. 
                          A是Address,B是Binding,C是Contract. Address可以是一个完整的URL,或是一个相对于基址的URL,如果为空,则使用基址.Binding指定通信模型比如:http,tcp,mssq等. 
                          Contract就是服务的contract(就是前面我们使用[OperationContract]修饰的那个服务接口). 第20行,是元数据的<endpoint>,用与发布一些元数据信息. 先到这吧..... WCF(五) Host WCF Service 这次我们讨论Host a WCF service. 前述 为了让client可以使用service,我们必须host service到一个每时每刻都在运行的环境中(我们叫Host 
                          Application),因为它时刻等待着来自client的request(请求). Host有人翻译成"寄宿",我仍使用Host. 
                          Host application负责start和endservice, 然后监听来自client的request,解析client的request,然后调用相应的service. 
                          最后把结果返回给client. 
   Host Application为每一个Service 创建一个ServiceHost对象. 当有多个Service是,就会创建多个ServiceHost对象. 
                          当client需要使用多种protocol(通信协议),如Http和tcp,此时你不必创建多个service, 
                          因为前面我们知道一个Service可以有多个endpoint(endpoint 包含Address,Binding,Contract). 
                          Binding指定不同的protocol,所以,我们只需创建多个endpoint,在binding中指定不同的protocol即可. 可以把service host到以下环境中: 1.IIS:我们需要创建一个Web Site,使用该Website去host wcf,然后将该website放到IIS中. 
                          如果你使用了IIS去host wcf,那么只能使用http protocol. 当有request到来时,host 
                          application可以自动激活,并开始监听. 2.WAS:(Windows Process Activation Service)作为IIS7.0的一个新特性.我们同样需要创建一个web 
                          site .Vista,Windows 7, Windows Server2008 等操作系统可以安装WAS. 
                          如果你使用了WAS去host wcf,可以使用http ,tcp,MSMQ,Named Pipe protocol. 
                          当有request到来时,host application可以自动激活,并开始监听. 3.Managed Application(托管程序,如Console,WinFrom,WPF): Self-Hosting. 
                          当有request到来时,host application不能自动激活.需要在代码中手动new出serviceHost对象. 我们如何选择哪一种host方式呢? 答案是:取决于你的操作系统,和通信需求,和特定场景. 1.如果你的操作系统是windows server2003,并且程序只用http即可满足需求. 那么使用IIS去host. 
                          如果程序除了使用http不可满足需求(除了http,还需要tcp),则要创建window 服务去host. 2.如果你的操作系统是windows server2008(或 windows 7 )使用WAS. 因为它可以支持更多的protocol. 3.如果仅仅用于演示,可以使用Console去host. 但是release到真正的产品时,最好用winForm或WPF. 对于每一种方式,我们如何实现呢? |