本地WCF客户端服务器连接:没有服务正在侦听的消息

时间:2011-11-26 23:39:26

标签: .net wcf wcf-4

两年后,我回来实施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

1 个答案:

答案 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)