C# 如何在windows服务中承载具有.net.tcp绑定的两个WCF服务

C# 如何在windows服务中承载具有.net.tcp绑定的两个WCF服务,c#,windows,wcf,C#,Windows,Wcf,我想主持两项服务 服务1(位于D驱动器上) 基于xml中配置的配置处理数据 净。tcp://ServerIP/Pune_service 服务2(位于E驱动上) 基于xml中配置的配置处理数据 净。tcp://ServerIP/Mumbai_service 现在,我尝试在两个不同的Windows服务中使用net.tcp绑定托管这些服务 Windows服务1已成功启动 但是当尝试启动第二个windows服务时,我遇到了错误,即。 AddressReadyUseException string h

我想主持两项服务

  • 服务1(位于D驱动器上) 基于xml中配置的配置处理数据 净。tcp://ServerIP/Pune_service
  • 服务2(位于E驱动上) 基于xml中配置的配置处理数据 净。tcp://ServerIP/Mumbai_service
  • 现在,我尝试在两个不同的Windows服务中使用net.tcp绑定托管这些服务 Windows服务1已成功启动 但是当尝试启动第二个windows服务时,我遇到了错误,即。 AddressReadyUseException

      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();
    
    以下是AddressReadyUseException的地址

    我想你可以搬走

    m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
    
    您正在尝试为tcp绑定添加第二个IMetadataExchange协定

    您还需要添加

    mBehave.HttpGetEnabled = true;
    

    获取mex信息。

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

    Binding mexBinding = MetadataExchangeBindings.CreateMexTcpBinding();
    CustomBinding mexBinding2 = new CustomBinding(mexBinding);
    mexBinding2.Elements.Find<TcpTransportBindingElement>().PortSharingEnabled==false //true
    
    下面是关于这个话题的相关问题

    您还可以对以下行进行注释,以确保代码正确运行

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

    我尝试了谷歌的所有解决方案,但没有成功。我正在发布我的代码,也请帮助..谢谢你的回复。。我已经按照你的建议更新了我的代码,但是我得到了相同的错误。似乎你也为每个服务使用相同的端口。即使启用了端口共享,也会出现相同的错误。我已经尝试过你的代码,如果我更改了服务的端口,它对我有效。嘿,谢谢你的评论。我们可以更改TCP端口号吗?如何更改?例如;字符串tcpBaseAddress=“net.tcp://”+\u szServerIP+“/”+\u szCurruntLocation+“\u FileServer”;可以是字符串tcpBaseAddress=“net.tcp://”+\u szServerIP+”:8888“+”/“+\u szCurruntLocation+”\u FileServer”;我已经试过了。我得到了错误:TCP错误10013谢谢你的回复。。。我已经做了你建议的改变。。。但我得到的地址为AddressReadyUseException时出错。传输管理器未能侦听。
    tcpb.MaxConnections = 10;
    tcpb.PortSharingEnabled = true;