c# - 当有多个服务可用时 WCF 服务无法启动

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

我正在从一个 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 中的服务配置编辑器来创建它。

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>

最佳答案

用于第一次服务

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

用于第二个服务

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

地址应该是唯一的

关于c# - 当有多个服务可用时 WCF 服务无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2412658/

相关文章:

c# - 如何使用多个线程分段下载文件

c# - Silverlight 3 中 TypeDescriptor 的替代品?

python - 如何使用谷歌应用引擎处理异常

ios - Swift 2.0 异常处理

c# - 异步测试在 Nunit 2.6.2 中挂起

c# - 好的/简单的例子 asp :gridview describing all its functionalities?

wcf - Set-WebConfigurationProperty失败

python - 如何在 spyne 中使用多个装饰器

c# - 如何从响应中删除 "method"节点?

c# - 异常在 try/catch 上下文中丢失了部分堆栈跟踪