WCF分布式开发常见错误(21):unable to open its IChannelListener.分发器未能打开侦听器

来源:互联网 发布:北京银行学知支行 编辑:程序博客网 时间:2024/05/16 17:51
Posted on 2009-07-27 01:31 Frank Xu Lei 阅读(1390) 评论(15)  编辑 收藏 网摘 所属分类: WCF分布式开发常见错误, SOA and EAI
   我在进行WCF基于 UserName 和 Passoword的安全验证编程的时候遇到这个错误:
The ChannelDispatcher at 'http://localhost:8001/WCFService' with contract(s) '"IWCFService"' is unable to open its IChannelListener. 'http://localhost:8001/WCFService' with contract(s) '"IWCFService的通道分发器未能通道打开侦听器。WCF安全问题一直困扰很多人,相信很多WCF学习者也对此非常头疼。这个错误我尝试了很久都没有解决。后来我把此问题发到WCF英文论坛,有个老外Rodrigo回答了我问题,但是没有直接答案,算是给了我一个提示。因为之前我猜测WCF安全模式里可以不适用证书。因为WSE3.0里我记得就不需要使用。他告诉了WCF安全强制要求提供证书【1】原问题地址:http://social.microsoft.com/Forums/zh-CN/wcf/threads:
I have a question for WCF UserName and Passoword Validation.
I use Message security mode,but and NetTcpBinding,Console Selfhost.
But when I run the host application,Then throwed an exception:
The ChannelDispatcher at 'net.tcp://localhost:9001/WCFService' with contract(s) '"IWCFService"' is unable to open its IChannelListener.
  I have try to solve it myself,But ,all methods are helpless,When I remove the codes in the config file it is ok :
        <binding  name="MessageAndUserName"   >
         
          <security mode="Message">
            <message algorithmSuite="Basic256" clientCredentialType="UserName"/>
          </security>
        </binding>
 is there anyone can give some tips?
Thanks a lot
错误截图:

-----------------------------------------------------------------
【2】原因:
       因为这里WCF服务使用的安全模型为:Message。WCF要求消息安全必须提供证书支持。而配置文件里没有设置。所以才出现这个错误。
     我搜索的此错误的很多帖子,基本问题和解决方法都不清晰,导致这个问题的原因很多。我最后把这个问题发到WCF的英文论坛,期待能有帮助的答案出现。

【3】解决办法:
    要解决这个问题,根本还是需要解决证书的配置问题。因为使用的是XP环境开发,所以证书配置比较麻烦,以前在做WSE3.0文章的时候,编写代码,也曾经详细学习过证书的制作。也准备了文章WSE3.0构建Web服务安全(2):非对称加密、公钥、密钥、证书、签名的区别和联系以及X.509 证书的获得和管理,记录了详细的证书制作过程。
   今天这里还是要使用到证书,主要问题就是要制作一个证书,然后在配置文件里进行配置。主要步骤如下:
(1)制作证书:
Microsoft Visual Studio 2008-->Visual Studio Tools-->Visual Studio 2008 命令提示行里输入:
makecert -r -pe -n "CN=FrankWCFServer" -ss My -sky exchange

具体的参数说明:详细帮助文档:http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cptools/html/cpgrfcertificatecreationtoolmakecertexe.asp
------------------------------------------------------------------------------------------------

-sr CurrentUser  --      证书存储位置。Location 可以是 currentuser(默认值)或 localmachine

-ss My  --                   证书存储名称,输出证书即存储在那里。My表示保存在“个人”

-n CN=MyTestCert --    证书名称。此名称符合 X.500 标准。最简单的方法是在双引号中指定此名称,并加上前缀 CN=;例如,"CN=FrankXu"。

-sky exchange --         指定颁发者的密钥类型,必须是 signature、exchange 或一个表示提供程序类型的整数。默认情况下,可传入 1 表示交换密钥,传入 2 表示签名密钥。

-pe --                         私钥标记为可导出。这样私钥包括在证书中。

-----------------------------------------------------------------------------------
这个命令生成一个名字为FrankWCFServer的证书,被保存到了当前用户的个人证书存储区内。一般我们自己开发使用的Windows证书服务来管理和获取证书。
运行成功以后,这里可以再浏览器--工具--选项里查看证书:


(2)设置证书:在WCF服务的配置文件里,设置对应的证书。直接在WCF服务端证书节点里添加如下代码即可:
 <serviceCertificate  x509FindType="FindBySubjectName" findValue="FrankWCFServer" storeLocation="CurrentUser"/>
   整个服务的参考代码如下:
<services>
      
<service behaviorConfiguration="WCFService.WCFServiceBehavior"
        name
="WCFService.WCFService">
        
<endpoint 
          address
="http://localhost:8001/WCFService" 
          binding
="wsHttpBinding"  bindingConfiguration="MessageAndUserName" contract="WCFService.IWCFService">
        
</endpoint>
        
        
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        
<host>
          
<baseAddresses>
            
<add baseAddress="http://localhost:8002/"/>
          
</baseAddresses>
        
</host>
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="WCFService.WCFServiceBehavior">
          
<serviceMetadata httpGetEnabled="true" />
          
<serviceDebug includeExceptionDetailInFaults="false" />
          
<serviceCredentials>
            
<serviceCertificate  x509FindType="FindBySubjectName" findValue="MyServer" storeLocation="CurrentUser"/>
            
            
<clientCertificate >
              
<authentication certificateValidationMode="None"  />
            
</clientCertificate>
            
<userNameAuthentication userNamePasswordValidationMode="Custom"    customUserNamePasswordValidatorType="WCFService.MyUserNamePasswordValidator,WCFService" />
          
</serviceCredentials>
        
</behavior>
       
      
</serviceBehaviors>
    
</behaviors>
    
<bindings>
      
<wsHttpBinding>
        
<binding  name="MessageAndUserName"   >
          
<security >
            
<message clientCredentialType="UserName"/>
          
</security>
        
</binding>
      
</wsHttpBinding>
    
</bindings>
     
</system.serviceModel> 
【4】总结:
   WCF安全开发一直是比较复杂的问题,因为不论相对于以前的Web Service 在Soap Header消息里封装UserName和Password的简单方式 ,还是和WSE3.0的直接提供 UserName和Password的Validation比较,都复杂了不少,这里主要涉及的是证书的设置和使用。对很多只是在XP环境下开发的学习者来说,证书的制作和安装已经是困难重重,何况还要理解一堆证书相关的安全的概念,包括加密算法、签名等一系列概念,很多人在学WCF安全开发的时候都会感觉无所适从,遇到问题也很难解决。详细的资料和可执行示例代码很少,几乎权威的参考文档都是英文。理解也非常困难。这个错误相信很多朋友也遇到过,就整理出来大家一起参考。        

参考资料:
http://msdn.microsoft.com/zh-cn/library/cc949011(en-us).aspx
http://social.microsoft.com/Forums/zh-CN/wcf/threads


【老徐的博客】

【作者】:Frank Xu Lei

【地址】:http://www.cnblogs.com/frank_xl/archive/2009/07/27/1531645.html

原创粉丝点击