2012-05-14 139 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