简体   繁体   English

WCF-使用NetTcpBinding设置多个WCF服务

[英]WCF - setting up more than 1 WCF Service with NetTcpBinding

I'm having hard time trying to expose multiple WCF Services. 我很难尝试公开多个WCF服务。 I host WCF Services in my server console app and after running the app I can successfuly open the ServiceHost endpoints. 我在服务器控制台应用程序中托管WCF服务,运行该应用程序后,我可以成功打开ServiceHost端点。 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: 我已经成功为一个客户端成功添加了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()

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 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. 更改端口号,例如,将第二个端点更改为端口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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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