【问题标题】:Client Cannot Connect to Self-Hosted WCF Service Started with no BaseAddress客户端无法连接到没有 BaseAddress 启动的自托管 WCF 服务
【发布时间】:2011-01-08 20:37:44
【问题描述】:

我创建了一个服务,但没有在 servicehost 构造函数中提供基地址。 并通过调用 AddServiceEndpoint 方法将端点添加到主机。在 AddServiceEndpoint 方法中,我提供了服务的完整地址。在客户端代理上,调用失败,异常为“在http://localhost:8000/Test/TestService 处没有可以接受消息的端点侦听。这通常是由不正确的地址或 SOAP 操作引起的。有关更多详细信息,请参阅 InnerException(如果存在)。 " 下面是ServiceHost和Client proxy的示例代码

using (ServiceHost host = new ServiceHost(typeof(Test.TestService)))
{
    host.AddServiceEndpoint(typeof(Test.ITestService),
    new BasicHttpBinding(), "http://localhost:8000/Test/TestService");
    Console.ReadLine();
    host.Close();
}

客户端代理:

EndpointAddress ep = new EndpointAddress("http://localhost:8000/Test/TestService");
ITestService proxy = ChannelFactory<ITestService>.CreateChannel(new BasicHttpBinding(), ep);
string s = proxy.HelloWorld();

我调用 proxy.Helloworld 的那一行失败,异常来了。

但是当我使用基地址创建 Servicehost 时,一切似乎都运行良好。 下面是我在 servicehost 构造函数中提供基地址的代码。

using (ServiceHost host = new ServiceHost(typeof(Test.TestService),new Uri("http://localhost:8000/Test")))
{
    host.AddServiceEndpoint(typeof(Test.ITestService),
    new BasicHttpBinding(), "/TestService");
    Console.ReadLine();
    host.Close();
}

谁能回答这里出了什么问题? : 我的运营合同如下:

[ServiceContract]public interface ITestService
{
    [OperationContract]
    string HelloWorld();
}
public class TestService: ITestService
{
    public string HelloWorld()
    {
        return "Hello World";
    }

}

问题是我没有使用任何配置文件,当在 servicehost 构造函数中提供基地址时一切正常,但是当我没有在 servicehost 构造函数中提供基地址并在 AddServicepoint 方法异常中提供完整的 uri 时来了。

【问题讨论】:

    标签: wcf wcf-client


    【解决方案1】:

    因为你从不打电话

    host.Open();

    在问题中显示的任何一个代码 sn-ps 中,我都不希望它们中的任何一个都能工作。

    您确定您发布的代码与导致您出现问题的代码完全相同吗?

    【讨论】:

    • 这回答了我的问题。我没有在第二种方法中调用 host.open,这就是异常出现的原因。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多