我有一段导致异常的代码。异常只抛出一次,场景无法复制。但我仍然想理解这个错误。这是代码:
1 Dim myHost As System.ServiceModel.ServiceHost = New ServiceHost(New ClientService())
2 myBinding = New NetTcpBinding(SecurityMode.None)
3 Dim clientAddress As String = "net.tcp://127.0.0.1:3500/Client"
4 myHost.AddServiceEndpoint(GetType(IClientService), myBinding, clientAddress)
IP 地址仅作为示例。在生产中,它是客户端机器的有效 IP 地址。在第 4 行抛出异常。这是堆栈:
1 System.UriFormatException: Invalid URI: The hostname could not be parsed.
2 at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
3 at System.Uri..ctor(String uriString, UriKind uriKind)
4 at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, String address, Uri listenUri)
5 at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, String address)
问题:
如果我只调用AddServiceEndpoint(Type implementContract, Binding binding, String address) ,为什么会调用重载AddServiceEndpoint(Type implementContract, Binding binding, String address, Uri listenUri ) ?
该异常的可能原因是什么?
谢谢你。
编辑:
clientAddress 字符串的实际代码是:
clientAddress = String.Format(My.Resources.UrlTemplate,ip.AddressList(0).ToString(), CInt(Int((1024 - 808 + 1) * Rnd() + 808)))
在哪里
Dim ip As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
为了清楚起见,我简化了第一个代码示例。Resources.resx 是生产代码中的嵌入资源。