两年后,我回来实施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
答案 0 :(得分:3)
您传递给ChannelFactory
的构造函数的端点地址不正确。服务基地址为http://localhost:8015/OnlineLicence,您在主机中添加的端点的相对地址为OnlineLicenceCommunicator
,因此端点地址为http://localhost:8015/OnlineLicence/OnlineLicenceCommunicator。
Dim factory As New ChannelFactory(Of IOnlineLicenceCommunication)(New BasicHttpBinding)
Dim address As New EndpointAddress("http://localhost:8015/Onlinelicence/OnlineLicenceCommunicator")
Dim client = factory.CreateChannel(address)