当有多个服务可用时,WCF服务无法启动

5

我正在从exe文件内部运行WCF服务(用于调试,将在部署时移至Windows服务)。我已经成功地运行了一个服务,但当我运行第二个服务时,会出现异常。

System.InvalidOperationException was unhandled
  Message=The ChannelDispatcher at 'http://backupsvr:8082/' with contract(s) '"IHttpGetHelpPageAndMetadataContract"' is unable to open its IChannelListener.
  Source=System.ServiceModel
  StackTrace:
       at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.Channels.CommunicationObject.Open()
       at Service.Program.Main() in E:\Visual Studio 2010\Projects\Contract Flow Suite\Service\Program.cs:line 30
  InnerException: System.InvalidOperationException
       Message=A registration already exists for URI 'http://backupsvr:8082/'.
       Source=System.ServiceModel
       StackTrace:
            at System.ServiceModel.Channels.UriPrefixTable`1.RegisterUri(Uri uri, HostNameComparisonMode hostNameComparisonMode, TItem item)
            at System.ServiceModel.Channels.HttpTransportManager.Register(TransportChannelListener channelListener)
            at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
            at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
            at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
            at System.ServiceModel.Channels.HttpChannelListener.OnOpen(TimeSpan timeout)
            at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
            at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
       InnerException: 

这里是调用它的代码。

using(ServiceHost hostRemoteUserManager = new ServiceHost(typeof(RemoteUserManager)))
using(ServiceHost hostDatabaseManagement = new ServiceHost(typeof(DatabaseManagement)))
try
{
    // Open the ServiceHost to start listening for messages.
    hostRemoteUserManager.Open();
    hostDatabaseManagement.Open(); //Exception on this line.
    // The service can now be accessed.
    Console.WriteLine("The service is ready.");
    Console.WriteLine("Press <ENTER> to terminate service.");
    Console.ReadLine();

    // Close the ServiceHost.
    hostRemoteUserManager.Close();
    hostDatabaseManagement.Close();
}

这是我的App.config文件,我使用Visual Studio 2010中的Service Configuration Editor创建它。

REMOVED

在我的App.config文件中,我需要更改什么才能允许多个服务而不是在不同端口上运行它们。当我使用“添加服务引用”工具时,我希望查询http://backupsvr:8082/并列出所有可用的服务。
更新 -
我执行了Igor的建议,现在它在同一端口上运行,但在添加服务引用对话框中,我仍然需要输入http://backupsvr:8082/RemoteUserManagerhttp://backupsvr:8082/DatabaseManagement而不是只有一个http://backupsvr:8082/。我不知道我想要的是否可行,因为对话框的设计方式似乎是这样的。这是我的app.config文件的更新副本。
<?xml version="1.0"?>
<configuration>
<system.diagnostics>
<sources>
  <source propagateActivity="true" name="System.ServiceModel" switchValue="Off,ActivityTracing">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
    </listeners>
  </source>
  <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
    <listeners>
      <add type="System.Diagnostics.DefaultTraceListener" name="Default">
        <filter type="" />
      </add>
    </listeners>
  </source>
</sources>
</system.diagnostics>
<system.serviceModel>
<bindings>
  <mexHttpBinding>
    <binding name="MexBinding" />
  </mexHttpBinding>
</bindings>
<diagnostics>
  <messageLogging logMalformedMessages="false" logMessagesAtServiceLevel="false"
    logMessagesAtTransportLevel="false" />
</diagnostics>
<behaviors>
  <serviceBehaviors>
    <behavior name="RemoteUserManagerBehavior">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
    <behavior name="DatabaseManagementBehavior">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<services>
  <service behaviorConfiguration="RemoteUserManagerBehavior" name="Service.RemoteUserManager">
    <endpoint address="" binding="netTcpBinding"
      bindingConfiguration="" name="RemoteUserManagerBinding" contract="Service.IRemoteUserManager" />
    <endpoint address="mex" binding="mexHttpBinding"
      bindingConfiguration="MexBinding" name="RemoteUserManagerMetadata"
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://backupsvr:8082/RemoteUserManager" />
        <add baseAddress="net.tcp://backupsvr:8081/RemoteUserManager" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="DatabaseManagementBehavior" name="Service.DatabaseManagement">
    <endpoint address="" binding="netTcpBinding"
      bindingConfiguration="" name="DatabaseManagementBinding" contract="Service.IDatabaseManagement" />
    <endpoint address="mex" binding="mexHttpBinding"
      bindingConfiguration="MexBinding" name="DatabaseManagementMetaData"
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://backupsvr:8082/DatabaseManagement" />
        <add baseAddress="net.tcp://backupsvr:8081/DatabaseManagement" />
      </baseAddresses>
    </host>
  </service>
</services>
</system.serviceModel>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>

为每个服务使用不同的基地址:<add baseAddress="http://backupsvr:8082/" />和第二个<add baseAddress="http://backupsvr:8081/" />在一个端口上无法拥有多个服务(一个服务实现有多个终结点,但只有一个端口)。 - garik
@Igor,如果您只能使用一个服务,那么为什么在使用添加服务引用时要从“可用服务”列表中选择单击呢? - Scott Chamberlain
为了确定错误,当您交换2个Open()调用的顺序或仅运行第二个调用时,它是否有效? - H H
1个回答

7

用于第一项服务

<baseAddresses>
      <add baseAddress="net.tcp://backupsvr:8082/IRemoteUserManager"/>
    </baseAddresses>

用于第二个服务

<baseAddresses>
      <add baseAddress="net.tcp://backupsvr:8082/IDatabaseManagement"/>
    </baseAddresses>

地址应该是唯一的。


1
你是对的,虽然我理解Scott的问题。在服务配置中使用“address”部分似乎是不必要的。我还看到过使用两个不同端口号(8082、8083)的解决方案。 - H H
现在它可以在同一个端口上运行。但是我必须调用http://backupsvr:8082/DatabaseManagement和http://backupsvr:8082/RemoteUserManager来获取它们的接口。如果我只需调用根目录并同时显示它们,那就太好了。我将使用我的更改更新原始帖子。 - Scott Chamberlain
我从未尝试过这样做,我希望您能通过一个类MySuperManagement来实现两个接口。我猜这样做是可行的。无论如何,您的Web服务客户端应该明白在哪个端口/地址上使用哪个服务(可以使用不同的端口或不同的地址)。 - garik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接