本地 WCF 客户端服务器连接:没有服务正在侦听的消息
两年后,我重新开始实施 WCF 服务。我希望初学者能够配置一个超级简单、无需配置文件的服务。我有下面的服务器代码。当我使用 svcutil 创建代理时一切都很好。但是,当我尝试自己使用 ChannelFactory 实现客户端时,我一直受到没有服务正在侦听的消息的困扰......错误在哪里?
客户端
Module OnlineLicenceClientConsole
Sub Main()
Console.WriteLine("Press enter to connect...")
Console.ReadLine()
Dim factory As New ChannelFactory(Of IOnlineLicenceCommunication)(New BasicHttpBinding)
Dim address As New EndpointAddress("http://localhost:8015/Onlinelicence")
Dim client = factory.CreateChannel(address)
Console.WriteLine("Client running...")
Do While (True)
Dim computerID = Console.ReadLine()
Dim request = New LicenceRequest With {.ComputerID = computerID, .CustomerID = "X", .ServiceID = "Y"}
Console.WriteLine(client.GetLicence(request).StatusMessage)
Loop
End Sub
End Module
主机
Module OnlineLicenceServerConsole
Sub Main()
Dim baseAddress As New Uri("http://localhost:8015/OnlineLicence")
Dim host = New ServiceHost(GetType(OnLineLicenceCommunicator), baseAddress)
Dim serviceBehavior As New ServiceMetadataBehavior With {.HttpGetEnabled = True}
host.Description.Behaviors.Add(serviceBehavior)
host.AddServiceEndpoint(
GetType(IOnlineLicenceCommunication),
New BasicHttpBinding,
"OnlineLicenceCommunicator")
Try
host.Open()
Console.WriteLine("Service running")
Console.ReadLine()
Catch e As CommunicationException
Console.WriteLine("Fout: {0}", e.Message)
Console.ReadLine()
host.Abort()
Finally
host.Close()
End Try
End Sub
End Module
After two years I am coming back to implementing a WCF service. I want for starters to configure a super simple, configuration file free service. I have the servercode below. When I use svcutil to create a proxy all is fine. But when I try to implement a client myself using a ChannelFactory I keep being plagued by the message that no service is listening.... Where is the mistake?
The Client
Module OnlineLicenceClientConsole
Sub Main()
Console.WriteLine("Press enter to connect...")
Console.ReadLine()
Dim factory As New ChannelFactory(Of IOnlineLicenceCommunication)(New BasicHttpBinding)
Dim address As New EndpointAddress("http://localhost:8015/Onlinelicence")
Dim client = factory.CreateChannel(address)
Console.WriteLine("Client running...")
Do While (True)
Dim computerID = Console.ReadLine()
Dim request = New LicenceRequest With {.ComputerID = computerID, .CustomerID = "X", .ServiceID = "Y"}
Console.WriteLine(client.GetLicence(request).StatusMessage)
Loop
End Sub
End Module
The Host
Module OnlineLicenceServerConsole
Sub Main()
Dim baseAddress As New Uri("http://localhost:8015/OnlineLicence")
Dim host = New ServiceHost(GetType(OnLineLicenceCommunicator), baseAddress)
Dim serviceBehavior As New ServiceMetadataBehavior With {.HttpGetEnabled = True}
host.Description.Behaviors.Add(serviceBehavior)
host.AddServiceEndpoint(
GetType(IOnlineLicenceCommunication),
New BasicHttpBinding,
"OnlineLicenceCommunicator")
Try
host.Open()
Console.WriteLine("Service running")
Console.ReadLine()
Catch e As CommunicationException
Console.WriteLine("Fout: {0}", e.Message)
Console.ReadLine()
host.Abort()
Finally
host.Close()
End Try
End Sub
End Module
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您传递给
ChannelFactory
构造函数的端点地址不正确。服务基地址为http://localhost:8015/OnlineLicence,以及您在中添加的端点的相对地址主机是OnlineLicenceCommunicator
,因此端点地址是 http://localhost:8015/OnlineLicence/OnlineLicenceCommunicator。The endpoint address which you're passing to the constructor of
ChannelFactory
is incorrect. The service base address is http://localhost:8015/OnlineLicence, and the relative address of the endpoint you added in the host isOnlineLicenceCommunicator
, so the endpoint address is http://localhost:8015/OnlineLicence/OnlineLicenceCommunicator.