【问题标题】:WCF service fails to start when more than one service is available当有多个服务可用时,WCF 服务无法启动
【发布时间】:2011-01-25 15:22:44
【问题描述】:

我正在从 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();
}

这是我使用 Visual Studio 2010 中的服务配置编辑器创建的 App.config 文件。

REMOVED

除了在不同的端口上运行它们之外,我需要在我的 App.config 文件中进行哪些更改以允许多个服务。我想查询http://backupsvr:8082/ 并让它列出我使用“添加服务引用”工具时可用的所有服务。

更新 --

我做了 Igor 的建议,它现在在同一个端口上运行,但是在“添加服务引用”对话框中,我仍然需要输入 http://backupsvr:8082/RemoteUserManager 和 http://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>

【问题讨论】:

  • 为每个服务使用一个不同的基地址 backupsvr:8082" /> 和第二个 backupsvr:8081" /> 一个服务不可能有多个服务端口(许多端点,但一个服务实现一个端口)
  • @Igor,如果您只能拥有一项服务,为什么在使用添加服务参考时从“可用服务”列表中单击?
  • 要确定他的错误,当您切换 2 个 Open() 调用的顺序或仅运行第二个调用时,它是否有效?

标签: c# wcf exception wcf-binding app-config


【解决方案1】:

用于首次服务

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

用于第二次服务

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

地址应该是唯一的

【讨论】:

  • 你是对的,虽然我理解 Scotts 的问题。服务配置中的“地址”部分似乎没有必要。我还看到了具有 2 个不同端口号(8082、8083)的解决方案
  • 它现在在同一个端口上运行。但是我必须调用backupsvr:8082/DatabaseManagement 和backupsvr:8082/RemoteUserManager 来获取他们的接口。如果我可以调用根并让它们都出现,我会喜欢它。我将用我的更改更新原始帖子。
  • 我从未尝试过,我希望您通过一个 MySuperManagement 类来实现这两个接口。通过使用这种方式,我猜是可以做到的。 :)。您的 Web 服务的客户端应该以任何方式了解该端口/地址上使用的服务(使用不同的端口或不同的地址)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
相关资源
最近更新 更多