【问题标题】:EndpointNotFoundException when creating callback WCF service创建回调 WCF 服务时出现 EndpointNotFoundException
【发布时间】:2012-02-07 05:54:42
【问题描述】:

我正在尝试基于this example 为 WCF 回调服务进行自托管。

这是托管代码:

服务:

static void Main(string[] args)
    {
        using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8000/HelloWCF")))
        {
            // Set up a service endpoint [Contract, Binding, Address]
            host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001/HelloWCF") }, "HelloWCF");

            // Enable metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };

            host.Description.Behaviors.Add(smb);
            host.Open();

            Console.WriteLine("Ready...");
            Console.ReadLine();
        }
    }

客户:

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding >
                <binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" clientBaseAddress="http://locahost:8001/HelloWCF">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/HelloWCF/HelloWCF" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
                name="WSDualHttpBinding_IMessage" >
                <identity>
                    <userPrincipalName value="badasscomputing\menkaur" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

c#代码:

    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    class Sender : IMessageCallback, IDisposable
    {
        private MessageClient messageClient;

        public void Go()
        {
            InstanceContext context = new InstanceContext(this);
            messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");

            for (int i = 0; i < 5; i++)
            {
                string message = string.Format("message #{0}", i);
                Console.WriteLine(">>> Sending " + message);
                messageClient.AddMessage(message);
            }

        }

        public void OnMessageAdded(string message, DateTime timestamp)
        {
        }

        public void Dispose()
        {
            messageClient.Close();
        }
    }

    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
    class Listener : IMessageCallback, IDisposable
    {
        private MessageClient messageClient;

        public void Open()
        {
            InstanceContext context = new InstanceContext(this);
            messageClient = new MessageClient(context, "WSDualHttpBinding_IMessage");

            messageClient.Subscribe();
        }

        public void OnMessageAdded(string message, DateTime timestamp)
        {
            Console.WriteLine("<<< Recieved {0} with a timestamp of {1}", message, timestamp);
        }

        public void Dispose()
        {
            messageClient.Unsubscribe();
            messageClient.Close();
        }
    }

    static void Main(string[] args)
    {
        Listener l = new Listener();
        l.Open();

        Sender s = new Sender();
        s.Go();
    }

服务器正常启动。 运行客户端时,尝试调用任何服务器函数时会崩溃,但出现以下异常:

EndpointNotFoundException:There was no endpoint listening at http://localhost:8000/HelloWCF/HelloWCF that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

内部异常是:无法连接到远程服务器。

这不是防火墙的原因,因为我成功测试了一个没有回调函数的类似应用

这可能是什么原因?

更新

完整的源代码可以在这里下载:http://iathao.com/tmp/fullSource.zip

【问题讨论】:

  • 我也喜欢你的机器/域名badasscomputing

标签: c# wcf


【解决方案1】:

您似乎将端点托管在http://localhost:8000/HelloWCF,但您的客户端配置指向http://localhost:8000/HelloWCF/HelloWCF(注意:额外的“/HelloWCF”)

更新

您的客户端配置合同设置为CallbackService.IMessage,但您的代码中没有任何地方有 IMessage 合同的服务实现。

【讨论】:

  • 如果我将客户端端点设置为 localhost:8000/HelloWCF ,内部异常更改为 404。服务器正在运行
  • IMessage 是服务器端接口。它期望向IMessageCallback发送回调消息,在客户端实现
  • 那么你能把这个接口的服务契约和服务实现代码包括进来吗?
【解决方案2】:

通过这些小改动,您的代码可以在我的机器上正常运行。

客户端配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsDualHttpBinding>
                <binding name="WSDualHttpBinding_IMessage" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" />
                    <security mode="Message">
                        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                            algorithmSuite="Default" />
                    </security>
                </binding>
            </wsDualHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8000/HelloWCF" binding="wsDualHttpBinding"
                bindingConfiguration="WSDualHttpBinding_IMessage" contract="CallbackService.IMessage"
                name="WSDualHttpBinding_IMessage">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

起始码

namespace wcfStarter
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(typeof(Message), new Uri("http://localhost:8002/HelloWCF")))
            {
                // Set up a service endpoint [Contract, Binding, Address]
                host.AddServiceEndpoint(typeof(IMessage), new WSDualHttpBinding() { ClientBaseAddress = new Uri("http://locahost:8001") }, "HelloWCF");

                // Enable metadata exchange
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior() {HttpGetEnabled =true };

                host.Description.Behaviors.Add(smb);
                host.Open();

                Console.WriteLine("Ready...");
                Console.ReadLine();
            }
        }
    }
}

【讨论】:

  • 通过这些更改,我得到“HTTP 无法注册 URL http://+:80/Temporary_Listen_Addresses/9b0d36c7-c04e-4204-9a7c-eb887b03f3c9/ 因为 TCP 端口 80 正在被另一个应用程序。”
  • 只要把ClientBaseAddress改成不同的端口就可以了。
  • 我自己才意识到这一点。将 clientBaseAddress ="localhost:8001" 添加到 configuration/system.serviceModel/bindings/wsDualHttpBinding/binding 已经完成了。我的错误是设置 clientBaseAddress="locahost:8001/HelloWCF"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
相关资源
最近更新 更多