2012-01-10 102 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