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

发布于 2024-10-10 17:08:16 字数 1675 浏览 5 评论 0原文

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

I have created a service without giving the base address in the servicehost constructor.
and added the endpoint to host by calling AddServiceEndpoint method. In the AddServiceEndpoint method i am providing the complete address of the service. 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."
below is the sample code of ServiceHost and 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();
}

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.

But when I create the Servicehost with a base address, everything seems to work fine.
below is the code where i provide the base address in the servicehost constructor.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

乱世争霸 2024-10-17 17:08:16

由于您从未

调用host.Open();

在问题中显示的任何代码片段中

,因此我不希望它们中的任何一个都能工作。您确定您发布的代码与导致问题的代码完全相同吗?

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?

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