HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/92819ef8-81ea-4bd9-

今天在练习wcf时,客户端调用服务端方法时出现异常。如下:

 

未处理System.ServiceModel.AddressAlreadyInUseException

Message="HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/88c4ba08-ef25-4254-8811-870fffe5f9ea/,因为另一应用程序正在使用 TCP 端口 80。"

 

网络解决方法:

当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端口来作为回调端口,可是作为网络程序,是需要本机的管理员权限才能注册的。

 

解决代码:

ServiceReference1.IcalculatorDuplexClient cd = new IcalculatorDuplexClient(InstanceContext);

 

Uri uri = new Uri(@"http://localhost:8000/");

if (cd.Endpoint.Binding.Name == "WSDualHttpBinding")

{

((WSDualHttpBinding)cd.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,直到启动起来为止。

from:http://blog.sina.com.cn/s/blog_6e93f12e0100zk44.html

原文地址:https://www.cnblogs.com/SFAN/p/3428220.html