简体   繁体   中英

WCF - setting up more than 1 WCF Service with NetTcpBinding

I'm having hard time trying to expose multiple WCF Services. I host WCF Services in my server console app and after running the app I can successfuly open the ServiceHost endpoints. I've managed to successfuly add WCFTestService reference for one client but when I try to add WCFTestService2 reference(right click references->add service reference->find the service and click ok) for another client I'm getting weird errors:

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()

My host console app:

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>

If somebody could point me a direction to resolve this I would highly appreciate it.

You are trying to open two service hosts on the same port, which is not possible. Each service has to be on an unique port (and not colliding with any other occupied port).

Change your port number, eg change the second endpoint to be on port 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM