【问题标题】:How to host two WCF services with .net.tcp binding in windows Service如何在 Windows 服务中使用 .net.tcp 绑定托管两个 WCF 服务
【发布时间】:2018-08-27 05:49:53
【问题描述】:

我想托管两个服务

  1. 服务 1(位于 D 驱动器上) 基于 xml 中配置的配置处理数据 net.tcp://ServerIP/Pune_service
  2. 服务 2(位于 E 驱动器上) 基于 xml 中配置的配置处理数据 net.tcp://ServerIP/Mumbai_service

现在我尝试在两个不同的 Windows 服务中使用 net.tcp 绑定来托管这些服务 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();

【问题讨论】:

  • 我尝试了 Google 的所有解决方案,但没有奏效。我也在发布我的代码请帮助..

标签: c# windows wcf


【解决方案1】:

这是 AddressAlreadyInUseException 的link

我认为你可以删除;

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

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

还需要添加;

mBehave.HttpGetEnabled = true;

获取墨西哥信息。

【讨论】:

  • 感谢您的回复.. 我已根据您的建议更新了我的代码,但我收到了相同的错误..
  • 您似乎也为每个服务使用相同的端口。即使启用了端口共享,它也会得到相同的错误。我试过你的代码,如果我改变服务的端口,它对我有用。
  • 嘿感谢您的评论。我们可以更改 TCP 端口号吗?如何?
  • 例如;字符串 tcpBaseAddress = "net.tcp://" + _szServerIP + "/" + _szCuruntLocation + "_FileServer";可以是字符串 tcpBaseAddress = "net.tcp://" + _szServerIP + " :8888" +"/" + _szCuruntLocation + "_FileServer";
  • 我已经尝试过了。为此我收到错误:TCP 错误 10013
【解决方案2】:

据我所知,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;

【讨论】:

  • 感谢您的回复...我已按照您的建议进行了更改...但出现 AddressAlreadyInUseException 错误。传输管理器无法监听。
猜你喜欢
  • 2011-10-01
  • 2015-01-28
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多