2012-05-14 137 views
0

我有一個WCF服務(service1)。我在託管應用程序中託管了該服務的幾個實例(簡單的.NET應用程序)。我有另一個WCF服務(service2)在Windows服務中託管。 當運行我的應用程序時,所有service1實例都連接到service2,並且一切正常。但是,當service2嘗試連接到service1的任何實例時,就會出現一個異常:「在net.tcp:// localhost:8732/TestComponent_6a4009df-cc68-4cd9-9414-16737c734548上沒有端點監聽,可以接受該消息。由不正確的地址或SOAP動作引起。請參閱InnerException(如果存在)以獲取更多詳細信息。「無法連接到託管應用程序託管的多個WCF服務

所有的services1實例都有唯一的地址(參見uri中的guid),但類似的服務契約和綁定類型。我使用打開端口共享的netTCP綁定。

任何建議?

注意:當我在託管應用程序中託管唯一一個service1實例時,一切都很順利。我可以運行我的應用程序的幾個實例,也沒有錯誤。只有當我在一個應用程序中託管多個service1實例時,我遇到了問題。

有一些代碼:

服務1實例創建:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    Component = new TestComponent("net.tcp://localhost:8732/TestComponent", Component_OnMessageReceivedEvent); 
    Component.JoinServer(); 
    Component2 = new TestComponent("net.tcp://localhost:8732/TestComponent", Component_OnMessageReceivedEvent); 
    //guids is added to addresses in TestComponent constructor 
    Component2.JoinServer(); 
} 

它是如何工作的組件內部:

public void JoinServer() 
{ 
    this.StartComponentHosting(); 

    if (ServerClient != null) 
    { 
    ServerClient.Close(); 
    ServerClient = null; 
    } 

    ServerClient = new ServerClient(); 
    ServerClient.Open(); //conneting to service2 
    ServerClient.JoinComponent(this.ProviderInfo); //calling some method on service2 
} 

private void StartComponentHosting() 
{ 
    if (ComponentHost != null) 
    { 
    ComponentHost.Close(); 
    } 

    ComponentHost = new ServiceHost(this); 
    var portsharingBinding = new NetTcpBinding("NetTCPBindingConfig") { PortSharingEnabled = true }; 
    ComponentHost.AddServiceEndpoint(typeof(IComponent), portsharingBinding, this.Address); 
    ComponentHost.Open(); 
} 

回答

0
private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    Component = new TestComponent("net.tcp://localhost:8732/TestComponent", Component_OnMessageReceivedEvent); 
    Component.JoinServer(); 
    Component2 = new TestComponent("net.tcp://localhost:8732/TestComponent", Component_OnMessageReceivedEvent); 
    //guids is added to addresses in TestComponent constructor 
    Component2.JoinServer(); 
} 

從這個代碼段,這一切似乎您的service1實例正在使用相同的端口和地址。儘快,如果您不使用IIS,則無法在同一個端口上託管多個實例。

+0

我使用portharing。它允許使用相同的端口。我的服務器也可以在8732端口上工作。正如我在每個應用程序僅託管一個service1實例並運行多個應用程序時編寫的,一切正常,並且它在單個端口上工作。 – Antonio