好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

wcf基础知识之端口共享 portSharing

wcf基础知识之端口共享 portSharing

wcf基础知识之端口共享 portSharing

现在时间已经是凌晨一点了,我准备了端口共享的内容,但是因为时间太晚,明天还要上班,所以我们就不长篇大徐了,剪短的说明一下内容,让大家明白就可以了。

今天来说一下端口共享,什么是端口共享呢?在wcf中,所谓的端口共享其实就是一个服务的地址为http://127.0.0.1:80/calService,而另一个服务的地址也为http:127.0.0.1:80/weatherService,但是端口是一样的,在wcf中这其实是不能运行的。第一个服务启动以后,第二个服务如果要启动的话就会出现异常,为了说明wcf的端口共享,我们仍然是来举个简单的例子说明一下。

   1  <?xml version= "  1.0  "  encoding= "  utf-8  "  ?>
  2  <configuration>
  3    <system.serviceModel>
  4      <services>
  5        <service name= "  Chinaer.WcfDemo.Services.GuoService  " >
  6          <endpoint address= "  net.tcp://127.0.0.1:8080/guoService  "  bindingConfiguration= "  portSharing  "  binding= "  netTcpBinding  "  contract= "  Chinaer.WcfDemo.Contracts.IGuo  " ></endpoint>
  7        </service>
  8        <service name= "  Chinaer.WcfDemo.Services.GuoTwoService  " >
  9          <endpoint address= "  net.tcp://127.0.0.1:8080/guoTwoService  "  bindingConfiguration= "  portSharing  "  binding= "  netTcpBinding  "  contract= "  Chinaer.WcfDemo.Contracts.IGuoTwo  " ></endpoint>
 10        </service>
 11      </services>
 12  
 13      <behaviors></behaviors>
 14  
 15      <bindings>
 16        <netTcpBinding>
 17          <binding name= "  portSharing  "  portSharingEnabled= "  true  " ></binding>
 18        </netTcpBinding>
 19        
 20      </bindings>
 21      
 22    </system.serviceModel>
 23    
 24    
 25  </configuration> 

在配置文件中,我们采用两个控制台应用程序来分别启动每个服务,但是我们的配置文件都采用同样的内容。大家可以看到,这两个服务的端口地址都是8080.

现在我们来说明第一个控制台宿主,第二个和第一个一致,只是服务类型更改了而已。

   1   using   System;
   2   using   System.Collections.Generic;
   3   using   System.Linq;
   4   using   System.Text;
   5   using   System.ServiceModel;
   6   using   Chinaer.WcfDemo.Services;
   7   using   System.ServiceModel.Description;
   8  
  9   namespace   Chinaer.WcfDemo.ConsoleHost
  10   {
  11       class   Program
  12       {
  13           static   void  Main( string  [] args)
  14           {
  15               using  (ServiceHost guoHost =  new  ServiceHost( typeof  (GuoService)))
  16               {
  17  
 18                   if  (guoHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ==  null  )
  19                   {
  20                      ServiceMetadataBehavior metaDataBehavior =  new   ServiceMetadataBehavior();
  21                      metaDataBehavior.HttpGetEnabled =  true  ;
  22                      metaDataBehavior.HttpGetUrl =  new  Uri( "  http://127.0.0.1:8088/guoService/mex  "  );
  23                       guoHost.Description.Behaviors.Add(metaDataBehavior);
  24                   }
  25                  guoHost.Opened +=  delegate 
 26                   {
  27                      Console.WriteLine( "  guoService 启动成功  "  );
  28                   };
  29                   try 
 30                   {
  31                       if  (guoHost.State !=  CommunicationState.Opened)
  32                       {
  33                           guoHost.Open();
  34                           Console.Read();
  35                       }
  36                   }
  37                   catch   (CommunicationException ex)
  38                   {
  39                       guoHost.Abort();
  40                   }
  41                   catch   (Exception ex)
  42                   {
  43                       guoHost.Abort();
  44                   }
  45               }            
  46               Console.Read();
  47           }
  48       }
  49  } 

我们分别启动这两个控制台宿主程序,采用

采用启动新实例的方式我们可以分别调试这两个控制台宿主程序。

首先有一点要说明一下,就是配置文件中 <binding name="portSharing" portSharingEnabled="true"></binding> portSharingEnabled要更改为false。

 

出现了我们想要的结果,地址已经存在的异常信息,就是端口已经被占用,这就是我们今天要解决的问题。

我们把portSharingEnabled要更改为true 就可以解决这个端口的问题。

当然这个问题是针对的netTcp协议,我发现了几个小问题:

portSharingEnabled设置为true,只要第一个启动的宿主控制台设置为true就可以,第二个设置为false,也不影响程序的成功。

网上说tcp要实现端口共享,必须要开启netTcp 服务,但是我在我本机上没有开启这个服务也是可以正常的实现端口共享,不知道是技术进步了还是这个服务只是一个摆设。

说完了tcp协议,现在我们来说一下http协议,只要把配置文件的访问协议更改为http就可以实现http协议的转换,不得不说wcf的设计确实很强大。

 

   1  <?xml version= "  1.0  "  encoding= "  utf-8  "  ?>
  2  <configuration>
  3    <system.serviceModel>
  4      <services>
  5        <service name= "  Chinaer.WcfDemo.Services.GuoService  " >
  6          <endpoint address= "  http://127.0.0.1:80/guoService  "   binding= "  wsHttpBinding  "  contract= "  Chinaer.WcfDemo.Contracts.IGuo  " ></endpoint>
  7        </service>
  8        <service name= "  Chinaer.WcfDemo.Services.GuoTwoService  " >
  9          <endpoint address= "  http://127.0.0.1:80/guoTwoService  "   binding= "  wsHttpBinding  "  contract= "  Chinaer.WcfDemo.Contracts.IGuoTwo  " ></endpoint>
 10        </service>
 11      </services>
 12  
 13      <behaviors></behaviors>
 14  
 15      <bindings>
 16        <netTcpBinding>
 17          <binding name= "  portSharing  "  portSharingEnabled= "  false  " ></binding>
 18        </netTcpBinding>
 19        
 20      </bindings>
 21      
 22    </system.serviceModel>
 23    
 24    
 25  </configuration> 

请注意两点:

端口我设置的端口是80,这是iis的默认端口,并且我的iis版本是5.1 采用http协议,两个服务的端口号也相同

我们来看看会发生什么吧,我还是要重复一下,我的iis版本是5.1,而不是iis 6或iis 7.

也是那个地址被占用的异常信息,但是这次这个异常信息不是由应用程序引起的,因为我们启动第一个宿主程序的时候就出现了这个错误,这个异常信息是由iis引起的,因为iis 5 会独占80端口,我们的程序端口也是80,所以就引起了这个地址已被占用的异常信息。如果使用的是iis 6或iis 7,因为底层的实现机制不同,所以不会出现这个异常信息。

如果要解决iis 5下80端口在http协议下的端口共享,我至今没有找到解决方案,除非更改程序的端口以外。

好了,今天的主题就是端口共享问题,这是一个实际的问题所在,我们在日常开发中会经常不经意间遇到他,知道了问题所在,对于我们解决问题是会很有帮助。

 

说个励志话:今天不努力,明天脑袋空,别人开宝马,我在家中哭。

但愿你不会有这样的结果。

每天进步一点,一年就会进步一大步,十年就可以成功,君子当自强不息,君子当好好学习,每天进步

 

分类:  Wcf

作者: Leo_wl

    

出处: http://www.cnblogs.com/Leo_wl/

    

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权信息

查看更多关于wcf基础知识之端口共享 portSharing的详细内容...

  阅读:40次