2012-01-10 99 views
0

我试图做一个WCF回调服务,基于this example自托管。EndpointNotFoundException当创建回调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

+2

我还喜欢你的机器/域名* badasscomputing * – 2012-01-10 21:51:21

回答

1

有了这些小小的更改,您的代码在我的机器上就能正常工作。

客户端配置

<?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(); 
      } 
     } 
    } 
} 
+0

与这些变化,我得到“HTTP无法注册URL http:// +:80/Temporary_Listen_Addresses/9b0d36c7-c04e-4204-9a7c-eb887b03f3c9 /因为TCP端口80正在被另一个应用程序使用。 “ – 2012-01-11 13:42:40

+0

如果您只是将ClientBaseAddress更改为其他端口,则应该可以。 – Jontatas 2012-01-11 13:49:28

+1

刚刚意识到我自己。向配置/ system.serviceModel/bindings/wsDualHttpBinding/binding添加clientBaseAddress =“http:// localhost:8001”已完成它。我的错误是设置clientBaseAddress =“http:// locahost:8001/HelloWCF” – 2012-01-11 13:54:59

2

你出现在http://localhost:8000/HelloWCF被托管的端点,但您的客户端配置指向http://localhost:8000/HelloWCF/HelloWCF(注:额外的“/ HelloWCF”)

UPDATE

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

+0

如果我设置了客户端的端点为http://本地主机:8000/HelloWCF,内部异常更改为404。服务器IS正在运行 – 2012-01-11 07:03:29

+0

请参阅我的更新 – 2012-01-11 08:22:05

+0

IMessage是服务器端界面。它期望将回调消息发送到IMessageCallback,该消息在客户端上实现 – 2012-01-11 10:15:58