我已經編寫了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:9102
和http://192.168.1.11:9102
作爲基地址。
當在一臺機器上打開主機和客戶端應用程序時,它工作正常。當我將其中一個應用程序移動到其他機器時,在執行client.Open()
時,它會返回System.EndpointNotFoundException
。
任何想法,我可能做錯了什麼?我試着用net.tcp
綁定和它的結果相同。
的Windows防火牆在兩臺機器上
系統防火牆怎麼樣?您是否打開9102傳入的請求? – 2013-02-17 14:11:34