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

发布于 2024-12-18 13:22:03 字数 1817 浏览 2 评论 0原文

两年后,我重新开始实施 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2024-12-25 13:22:03

您传递给 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) 

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 is OnlineLicenceCommunicator, so the endpoint address is 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) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文