简体   繁体   English

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

[英]How to host two WCF services with .net.tcp binding in windows Service

i want to host two services 我想主持两项服务

  1. Service 1 (located on D Drive) process data based on configuration configured in xml net.tcp://ServerIP/Pune_service 服务1(位于D驱动器上)基于xml net.tcp:// ServerIP / Pune_service中配置的配置处理数据
  2. Service 2 (located on E Drive) process data based on configuration configured in xml net.tcp://ServerIP/Mumbai_service 服务2(位于E驱动器上)基于xml net.tcp:// ServerIP / Mumbai_service中配置的配置处理数据

now i tried to host these services with net.tcp binding in two different Windows Service Windows service 1 Started Successfully but when tried to start second windows service i'am getting Error ie AddressAlreadyInUseException 现在,我尝试在两个不同的Windows服务 Windows服务1中成功地将这些服务与net.tcp绑定一起托管,但是当尝试启动第二个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();

Here is the link for AddressAlreadyInUseException. 这是AddressAlreadyInUseException的链接

I think you may remove; 我认为您可以删除;

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

You are trying to add a second IMetadataExchange contract for tcp binding. 您正在尝试为TCP绑定添加第二个IMetadataExchange合同。

Also you need to add; 您还需要添加;

mBehave.HttpGetEnabled = true;

to get mex information. 获取mex信息。

As far as I know,MexTcpBinding disable the port sharing. 据我了解,MexTcpBinding禁用端口共享。 Which means that: 意思就是:

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

Here is an article worth reading. 这是一篇值得阅读的文章。
https://blogs.msdn.microsoft.com/drnick/2006/08/23/an-unanticipated-addendum-for-certain-mex-scenarios/ https://blogs.msdn.microsoft.com/drnick/2006/08/23/an-unanticipated-addendum-for-certain-mex-scenarios/
I suggest you could write the custom binding, and then add the binding to the mex endpoint. 我建议您可以编写自定义绑定,然后将绑定添加到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");

Here is related issue on this topic. 这是与此主题相关的问题。

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

you could also comment the following lines to ensure that codes run correctly. 您还可以注释以下几行以确保代码正确运行。

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

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

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