WCF IIS host support wsDualhttp协议问题

当WCF的服务使用session时,我们只有几种协议可供选择:
1. netTcp - 用这个协议是最好的,但有个重要问题是,IIS6不支持netTcp协议,所以要么我们用IIS7来host我们的WCF服务,要么就自己写一个host;
2. wsDualHttp - 因为Http协议天生不是双工通讯的,所以要想双工通讯,必须在客户端再启用一个端口来做回调端口,如果我们不在客户端的代理端做任何事情的话,很可能得到以下错误:
HTTP无法注册 URL http://+:80/Temporary_Listen_Addresses/08eee047-5225-4970-a777-084fe92620b9/。进程不具有此命名空间的访问权限(有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=70353)。

因为客户端默认要用Tcp 80端口来作为回调端口,可是作为网络程序,是需要本机的管理员权限才能注册的。
根据微软建议,需要做以下操作:

Running windows server 2003

自带httpcfg工具,直接运行
httpcfg set urlacl /u http://localhost:80 /aACL

Running Windows XP

需要下载tool at Windows XP Service Pack 2 Support Tools
或者找到Windows Server 2003 安装盘,安装目录\Support\Tools\Support.msi文件,然后在C:\Program Files\Support Tools目录下可以找到httpcfg.exe。
然后运行
httpcfg set urlacl /u http://localhost:80 /aACL
如果添加时出错,先用httpcfg query iplisten 查询该IP是否在侦听列表内。如果在就用httpcfg delete iplisten -i IP地址,先删除,然后httpcfg set iplisten -i IP地址 ,再加上去,重启IIS服务。

Running Windows Vista

If you are running on Windows Vista, you can use the Netsh.exe tool instead. The following shows an example of using this command.

netsh http add urlacl url=http://+:80/ user=username


因为Unfortunate is the fact that httpcfg.exe tool requires the user to enter a Security Descriptor Definition Language (SDDL) string by hand.
所以PaulWh提供了一个工具来解决这个问题。
我们可以用这个工具方便得添加一个端口:http://+:80/,然后添加用户yourMachineName\currentUserName,然后选中该用户,给其execute权限即可。

然而,对于客户端安装了IIS的用户来说,TCP 80端口已经默认被IIS 占用了(只有Windows XP有这个问题,Windows server 2003和Windows Vista不存在这个问题)。所以我们需要另更改一个可用的端口。
MyWCFServiceClient myClient =  new MyWCFServiceClient ();
Uri uri = new Uri(@"http://localhost:8000/");
if(myClient.Endpoint.Binding.Name == "WSDualHttpBinding")
{
  ((WSDualHttpBinding)myClient.Endpoint.Binding).ClientBaseAddress = uri ;
}

但是,这样还有个问题,如果这个端口也不幸被占用了或者我们需要在XP端同时执行2个及2个以上客户端程序的话,会报错误“HTTP无法注册URL http://+:8000/。另一应用程序已使用HTTP.SYS注册了该URL”。

所以有人提出了这样一个解决方案:
int selectedPort = 8000;
bool goodPort = false;
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
//
while (!goodPort)
{
 
  IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, selectedPort);
  TcpListener list = new TcpListener(ipLocalEndPoint);
  try
  {
    list.Start();
    goodPort = true;
    list.Stop();
  }
  catch
  {
    selectedPort++;
  }
}

上面这段程序通过尝试从8000起的端口,如果不能start就通过catch捕获到后,端口号自增1,直到启动起来为止。

这样,当端口能启动,但不能注册时,我们再用刚才那些工具将端口注册即可。

另外,如果不报端口错误,而程序长时间等待无响应的话,查看一下你的防火墙是否开着,如果你不想关闭的话,请将刚才添加的端口加到防火墙的例外端口里,这样就能正常访问了。

继续探索中。。。
原文地址:https://www.cnblogs.com/cubean/p/1571002.html