简体   繁体   English

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

[英]Client Cannot Connect to Self-Hosted WCF Service Started with no BaseAddress

I have created a service without giving the base address in the servicehost constructor. 我创建了一个服务,但没有在servicehost构造函数中给出基址。 and added the endpoint to host by calling AddServiceEndpoint method. 并通过调用AddServiceEndpoint方法将端点添加到主机。 In the AddServiceEndpoint method i am providing the complete address of the service. 在AddServiceEndpoint方法中,我提供了服务的完整地址。 On the client proxy the call is failing with the exception as "There was no endpoint listening at http://localhost:8000/Test/TestService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details." 在客户端代理上,调用失败,异常为“没有端点侦听http:// localhost:8000 / Test / TestService ,可以接受该消息。这通常是由不正确的地址或SOAP操作引起的。请参阅InnerException ,如果有的话,更多细节。“ below is the sample code of ServiceHost and Client proxy 下面是ServiceHost和Client代理的示例代码

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

Client Proxy: 客户端代理:

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

The line where I call proxy.Helloworld is failing and the exception comes. 我称之为proxy.Helloworld的行失败了,异常来了。

But when I create the Servicehost with a base address, everything seems to work fine. 但是当我使用基地址创建Servicehost时,一切似乎都能正常工作。 below is the code where i provide the base address in the servicehost constructor. 下面是我在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();
}

Can anyone answer what is going wrong here? 任何人都可以回答这里出了什么问题吗? : My Operation Contract is as below: :我的运营合同如下:

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

}

The problem is I am not using any config file and when provide the base address in the servicehost constructor everything works fine but when i don't provide the base address in the servicehost constructor and provide the complete uri in the AddServicepoint method exception comes. 问题是我没有使用任何配置文件,当在servicehost构造函数中提供基地址时,一切正常,但是当我不在servicehost构造函数中提供基地址并在AddServicepoint方法中提供完整的uri时,异常就出现了。

Since you never call 既然你从不打电话

host.Open();

in either of the code snippets shown in the question, I would not expect either of them to work. 在问题中显示的任何一个代码片段中,我都不希望它们中的任何一个起作用。

Are you sure that you have posted the exact same code that is causing you the problem? 您确定已发布导致问题的完全相同的代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM