客户端无法连接到没有BaseAddress启动的自托管WCF服务

时间:2011-01-08 20:37:45

标签: wcf wcf-client

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

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时来了。

1 个答案:

答案 0 :(得分:3)

因为你从不打电话

host.Open();

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

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