我很难尝试公开多个WCF服务。我在我的服务器控制台应用程序中托管WCF服务,运行应用程序后,我可以成功打开ServiceHost端点。我成功地为一个客户端添加了WCFTestService引用,但是当我尝试为另一个客户端添加WCFTestService2引用(右键单击引用 - >添加服务引用 - >查找服务并单击确定)时得到奇怪的错误:
System.ServiceModel.AddressAlreadyInUseException: Receiver already exists at the endpoint IP 0.0.0.0:8523. It could happen so if another application listens at this endpoint or service host contains many endpoints of services with the same endpoint IP but with invalid binding configurations---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/address/port) is normally allowed
in System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
in System.Net.Sockets.Socket.Bind(EndPoint localEP)
in System.ServiceModel.Channels.SocketConnectionListener.Listen()
--- End of inner exceptions stack trace ---
in System.ServiceModel.Channels.SocketConnectionListener.Listen()
in System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()
in System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
in System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
in System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
in System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)
in System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)
in System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
w System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
in System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
in System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
in System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
in Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
System.Net.Sockets.SocketException (0x80004005): Tylko jedno użycie każdego adresu gniazda (protokół/adres sieciowy/port) jest normalnie dozwolone
in System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
in System.Net.Sockets.Socket.Bind(EndPoint localEP)
in System.ServiceModel.Channels.SocketConnectionListener.Listen()
我的主机控制台应用:
static void Main(string[] args)
{
try
{
ServiceHost selfHost = new ServiceHost(typeof(contractImplementation1), new Uri("net.tcp://localhost:8523/WCFTestService"));
selfHost.Open();
selfHost = new ServiceHost(typeof(contractImplementation2), new Uri("net.tcp://localhost:8523/WCFTestService2"));
selfHost.Open();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occurred: {0}", ce.Message);
}
}
的app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServices.MyServiceBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WCFServices.MyServiceBehavior"
name="WcfServices.contractImplementation1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
name="NetTcpBindingEndpoint" contract="WcfServices.IcontractImplementation1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="MexTcpBindingEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/WCFTestService" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="WCFServices.MyServiceBehavior"
name="WcfServices.contractImplementation2">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="WcfServices.IcontractImplementation2">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/WCFTestService2" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
如果有人能指出我解决问题的方向,我将非常感激。
答案 0 :(得分:1)
您正尝试在同一端口上打开两个服务主机,这是不可能的。每个服务必须位于一个唯一的端口上(并且不与任何其他被占用的端口冲突)。
更改您的端口号,例如将第二个端点更改为端口8524.
ServiceHost selfHost = new ServiceHost(typeof(contractImplementation1),
new Uri("net.tcp://localhost:8523/WCFTestService"));
selfHost.Open();
selfHost = new ServiceHost(typeof(contractImplementation2),
new Uri("net.tcp://localhost:8524/WCFTestService2"));
selfHost.Open();