2013-02-17 50 views
0

我已經編寫了2個簡單的控制檯應用程序,一個託管wcf服務,另一個是wcf服務客戶端。使用LAN時自託管的WCF服務EndpointNotFoundException

如果我在一臺計算機(主機和客戶端)上運行這兩個應用程序,它可以正常工作,但是如果我嘗試將客戶端或主機移動到不同的計算機,則在嘗試連接到主機時會得到EndpintNotFountException

這是我服務的代碼:

namespace ConsoleApplication7 
{ 
    using System.ServiceModel; 

    [ServiceContract] 
    public interface ISimpleService 
    { 
     [OperationContract] 
     void Register(string name); 
    } 
} 

和執行類:

namespace ConsoleApplication7 
{ 
    public class SimpleService : ISimpleService 
    { 
     public void Register(string name) 
     { 
      DataBase.Save(name); 
     } 
    } 
} 

正如你看到它是非常簡單的服務:)

這是宿主程序的代碼:

using System; 
using System.ServiceModel; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     var service = new ServiceHost(typeof(SimpleService)); 
     service.Open(); 

     while (true) 
     { 
      Console.ReadLine(); 
      foreach (var s in DataBase.Get()) 
      { 
       Console.WriteLine(s); 
      } 
     } 
    } 
} 

它打開服務,當輸入按下時它將打印數據庫的內容。我知道它沒有Close()和其他東西,但它只是爲了顯示我的問題。

這裏是app.config文件:

<system.serviceModel> 
    <bindings> 
     <netTcpBinding> 
     <binding name ="myBinding"> 
      <security mode="None"></security> 
     </binding> 
     </netTcpBinding> 
    </bindings> 
    <client /> 
    <services> 
     <service name="ConsoleApplication7.SimpleService" behaviorConfiguration="ServiceBehavior"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="myBinding" contract="ConsoleApplication7.ISimpleService"/> 
     <endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication7.ISimpleService"/> 
     <endpoint address ="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:9101"/> 
      <add baseAddress="http://localhost:9102"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehavior" > 
      <serviceMetadata httpGetEnabled="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

客戶端代碼:

using System; 
using System.ServiceModel; 

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Click to open"); 
     Console.ReadLine(); 

     try 
     { 
      var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.1.11:9102/")); 
      service.Open(); 
      service.Register("itsMe"); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e); 
     } 

     Console.ReadLine(); 
    } 
} 

客戶是通過視覺工作室由AddServiceReference選項創建app.config。我試過http://localhost:9102http://192.168.1.11:9102作爲基地址。

當在一臺機器上打開主機和客戶端應用程序時,它工作正常。當我將其中一個應用程序移動到其他機器時,在執行client.Open()時,它會返回System.EndpointNotFoundException

任何想法,我可能做錯了什麼?我試着用net.tcp綁定和它的結果相同。

的Windows防火牆在兩臺機器上

+0

系統防火牆怎麼樣?您是否打開9102傳入的請求? – 2013-02-17 14:11:34

回答

0

當您移動客戶端或服務在不同的機器你不提更改服務地址禁用。您正在使用的地址 - localhost:xxxx只能在程序所在的機器上識別,例如,如果您的服務在Machine1上,並且您將客戶端移動到Machine2,則客戶端將無法將該服務視爲它會期望它在Machine2上。您可能會遇到與IP地址類似的問題,但看起來更像是路由器地址。

我建議更新服務的配置文件以使用機器名稱(例如,http://machine1:9102),並使用該地址從客戶端訪問服務。例如:

var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://machine1:9102/")); 
+0

我提到過我已經嘗試在主機app.config中將baseAddress設置爲「http:// localhost:9102」並將其設置爲「http://192.168.1.11:9102」(這是主機的IP地址)。 我還沒有嘗試使用機器名稱,但我不明白它是如何工作的,如果使用IP地址它不起作用? – Qti 2013-02-18 09:59:00