【发布时间】:2014-04-16 17:14:04
【问题描述】:
我有一个 WCF 服务,该服务在 WSHttp 端点上运行良好,在 ServiceHost 下作为 Windows 服务运行。但是,由于可扩展性,我想移动到 TCP 端点。对于我的生活,我无法弄清楚如何正确托管它。这是我的主机在 VB 中的 OnStart 例程:
Protected Overrides Sub OnStart(ByVal args() As String)
If _svcHost IsNot Nothing Then
_svcHost.Close()
End If
_svcHost = New ServiceHost(GetType(AutoTestsDataService), New Uri("net.tcp://" & GetIPv4Address() & ":8000"))
Dim metaDataBehavior = _svcHost.Description.Behaviors.Find(Of ServiceMetadataBehavior)()
If metaDataBehavior Is Nothing Then
_svcHost.Description.Behaviors.Add(New ServiceMetadataBehavior() With {.HttpGetEnabled = False})
Else
metaDataBehavior.HttpGetEnabled = False
End If
_svcHost.AddServiceEndpoint(GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding, "mex")
_svcHost.AddServiceEndpoint(GetType(AutoTestsDataService), New NetTcpBinding(SecurityMode.None, False), ServiceName)
Dim debugBehavior As ServiceDebugBehavior = _svcHost.Description.Behaviors.Find(Of ServiceDebugBehavior)()
If debugBehavior Is Nothing Then
_svcHost.Description.Behaviors.Add(New ServiceDebugBehavior() With {.IncludeExceptionDetailInFaults = My.Settings.flagDebug})
Else
debugBehavior.IncludeExceptionDetailInFaults = My.Settings.flagDebug
End If
Try
_svcHost.Open()
Catch ex As Exception
_svcHost.Abort()
End Try
End Sub
按原样,代码编译正常,安装正常,Windows 服务启动正常。但是端口 8000 上没有任何监听。我已确保 Net.Tcp 监听器和端口共享服务运行正常。我选择完全不使用配置文件,因为过去我遇到了很多问题,而且对我来说将代码放在配置文件中是不好的,不管微软想让我相信什么。无论如何,代码优先的实现总是比 XML 更容易理解,对我来说,上面的代码将所有正确的部分放在正确的位置。它只是拒绝工作。就像我说的我可以坚持使用 WSHttp,但我更愿意理解为什么 Net.Tcp 不起作用。
【问题讨论】:
-
_svcHost.Open()扔了吗?如果有,有什么例外? -
否定,一切正常,8000端口没有任何监听。根本没有抛出异常。
-
该链接指向 IIS 托管服务。我的是通过 ServiceHost 托管的,据我所知,没有要启用的协议。只要 Net.Tcp Listener 服务正在运行,它就应该可以工作。
标签: web-services wcf vb.net service