簡體   English   中英

WCF-使用NetTcpBinding設置多個WCF服務

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

我很難嘗試公開多個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>

如果有人能指出解決問題的方向,我將不勝感激。

您試圖在同一端口上打開兩個服務主機,這是不可能的。 每個服務都必須位於唯一的端口上(並且不得與任何其他占用的端口沖突)。

更改端口號,例如,將第二個端點更改為端口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