【问题标题】:System.ServiceModel.ServiceHost.AddServiceEndpoint constructor causes the Exception to be thrownSystem.ServiceModel.ServiceHost.AddServiceEndpoint 构造函数导致抛出异常
【发布时间】:2013-03-25 19:40:04
【问题描述】:

我有一段导致异常的代码。 异常只抛出一次,场景无法复制。但我仍然想理解这个错误。 代码如下:

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)

问题:

  1. 如果我只调用AddServiceEndpoint(Type implementContract, Binding binding, String address)?

  2. 可能导致该异常的原因是什么?

谢谢。

编辑:

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 是生产代码中的嵌入资源。

【问题讨论】:

    标签: vb.net wcf .net-3.5


    【解决方案1】:
    1. AddServiceEndpoint 有很多重载,通常的做法是让一个重载完成大部分工作,而其他(通常)采用较少参数并调用更完整的重载 - 这是如您在堆栈跟踪中看到的那样。 AddServiceEndpoint(Type, Binding, String) 内部调用 AddServiceEndpoint(Type, Binding, String, Uri)

    2. 您说它不再重现,因此如果没有实际值,很难猜测可能出了什么问题。根据异常,clientAddress 的值肯定有问题,但在不知道导致该问题的值的情况下,我们只能猜测。例如,如果您使用 "net.tcp://" 作为该变量的值,您可以获得相同的异常。

    【讨论】:

    • 谢谢@carlosfigueira。我只是认为 msdn 文档会包含该信息。至于第二点 - 请参阅我最近对帖子的编辑。再次感谢。
    猜你喜欢
    • 2011-11-04
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2017-11-07
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多