我正在从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/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>