如何在Windows Service中使用.net.tcp绑定托管两个WCF服务

时间:2018-08-27 05:49:54

标签: c# windows wcf

我要托管两项服务

  1. 服务1(位于D盘上) 根据xml中配置的配置处理数据 net.tcp:// ServerIP / Pune_service
  2. 服务2(位于E盘上) 根据xml中配置的配置处理数据 net.tcp:// ServerIP /孟买服务

现在,我尝试将net.tcp绑定的这些服务托管在两个不同的Windows服务中 Windows服务1已成功启动 但是当尝试启动第二个Windows服务时出现错误,即 AddressAlreadyInUseException

  string httpBaseAddress = "http://" + _szServerIP + "/" + _szCurruntLocation + "_FileServer";
                string tcpBaseAddress = "net.tcp://" + _szServerIP + "/" + _szCurruntLocation + "_FileServer";


                Uri[] adrbase = { new Uri(httpBaseAddress), new Uri(tcpBaseAddress) };
                m_svcHost = new ServiceHost(typeof(MyService.CalcServiceClient), adrbase);

                ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
                //mBehave.AddressFilterMode=AddressFilterMode.Any)]
                m_svcHost.Description.Behaviors.Add(mBehave);

                BasicHttpBinding httpb = new BasicHttpBinding();
                m_svcHost.AddServiceEndpoint(typeof(MyService.ICalcService), httpb, httpBaseAddress);
                m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");


                NetTcpBinding tcpb = new NetTcpBinding();
                tcpb.MaxConnections = 10;
                tcpb.MaxReceivedMessageSize = Int32.MaxValue;
                tcpb.MaxBufferPoolSize = Int32.MaxValue;
                tcpb.MaxBufferSize = Int32.MaxValue;
                tcpb.ReceiveTimeout = new TimeSpan(0, 10, 0);
                tcpb.OpenTimeout = new TimeSpan(0, 10, 0);
                tcpb.CloseTimeout = new TimeSpan(0, 10, 0);
                tcpb.PortSharingEnabled = true;

                m_svcHost.AddServiceEndpoint(typeof(MyService.ICalcService), tcpb, tcpBaseAddress);
                m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                m_svcHost.Open();


                Console.WriteLine("Service is live now at : {0}", httpBaseAddress);
                Console.ReadLine();

2 个答案:

答案 0 :(得分:0)

这是AddressAlreadyInUseException的link

我认为您可以删除;

m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

您正在尝试为TCP绑定添加第二个IMetadataExchange合同。

还需要添加;

mBehave.HttpGetEnabled = true;

获取mex信息。

答案 1 :(得分:0)

据我所知,MexTcpBinding禁用端口共享。这意味着:

Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
CustomBinding mexBinding2 = new CustomBinding(mexBinding);
mexBinding2.Elements.Find<TcpTransportBindingElement>().PortSharingEnabled==false //true

这里值得一读。
https://blogs.msdn.microsoft.com/drnick/2006/08/23/an-unanticipated-addendum-for-certain-mex-scenarios/
我建议您可以编写自定义绑定,然后将绑定添加到mex端点。

TextMessageEncodingBindingElement encoding = new TextMessageEncodingBindingElement();
            TcpTransportBindingElement transport = new TcpTransportBindingElement();
            transport.PortSharingEnabled = true;
            CustomBinding binding1 = new CustomBinding(encoding, transport);
            m_svcHost.AddServiceEndpoint(typeof(IService), tcpb, tcpBaseAddress);
            m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), binding1, "mex");

这是与此主题相关的问题。

https://blogs.msdn.microsoft.com/praburaj/2012/10/16/nettcpbinding-mextcpbinding-sharing-same-port-throws-addressalreadyinuseexception-on-upgrade-to-net-4-5/

您还可以注释以下几行以确保代码正确运行。

tcpb.MaxConnections = 10;
tcpb.PortSharingEnabled = true;