mono c#strange地址已被使用错误

时间:2014-01-04 12:52:18

标签: c# linux wcf tcp mono

这是我第一次使用mono,我编译了一些现成的代码并尝试在我的linux主机上使用但我得到了奇怪的错误。我并不是很熟悉C#专家,但我可以管理代码。

我忘了提及,没有任何一个应用程序使用端口5031.我甚至将端口更改为随机数,但它仍然给出相同的错误。

Unhandled Exception:
System.Net.Sockets.SocketException: Address already in use
  at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpListener.Start () [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.NetTcp.TcpChannelListener`1[System.ServiceModel.Channels.IDuplexSessionChannel].OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Dispatcher.ListenerLoopManager.Setup (TimeSpan openTimeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.ServiceHostBase.OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open () [0x00000] in <filename unknown>:0
  at H_Auth.AuthSvc.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: Address already in use
  at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpListener.Start () [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.NetTcp.TcpChannelListener`1[System.ServiceModel.Channels.IDuplexSessionChannel].OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Dispatcher.ListenerLoopManager.Setup (TimeSpan openTimeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.ServiceHostBase.OnOpen (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in <filename unknown>:0
  at System.ServiceModel.Channels.CommunicationObject.Open () [0x00000] in <filename unknown>:0
  at H_Auth.AuthSvc.Main (System.String[] args) [0x00000] in <filename unknown>:0

这是我的代码

namespace H_Auth
{
  internal class AuthSvc
  {
    private static void Main(string[] args)
    {
            var adrs = new Uri[1];
            adrs[0] = new Uri("net.tcp://localhost:5031/");
            using (ServiceHost serviceHost = new ServiceHost(typeof (HBChannel), adrs))
            {
      try
      {
        serviceHost.AddServiceEndpoint(typeof (IA), (System.ServiceModel.Channels.Binding) new NetTcpBinding(SecurityMode.None), "Auth.svc");
        ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
        serviceHost.Description.Behaviors.Add((IServiceBehavior) metadataBehavior);
        ((ServiceHostBase) serviceHost).AddServiceEndpoint("IMetadataExchange", MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
        serviceHost.Open();
        string str = Regex.Match(((AssemblyFileVersionAttribute) Assembly.GetEntryAssembly().GetCustomAttributes(typeof (AssemblyFileVersionAttribute), false)[0]).Version, "^\\d+\\.\\d+").Value;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Revision " + str + "\r\n");
        Console.ResetColor();
        Console.WriteLine("press \"S\" for stats print");
        Console.WriteLine();

        ConsoleKeyInfo consoleKeyInfo = new ConsoleKeyInfo();
        while (consoleKeyInfo.Key != ConsoleKey.Enter)
        {
                    consoleKeyInfo = Console.ReadKey(true);
                    if (consoleKeyInfo.Key == ConsoleKey.S)
                    {
                      AuthImpl.Instance.RemoveExpiredSessions();
                       AuthSvc.PrintStats(AuthImpl.Instance.GetSessions);
                     }
        }
        serviceHost.Close();
      }
      catch (CommunicationException ex)
      {
        Logging.Ex(ex.Message);
        serviceHost.Abort();
      }
            }
      Console.ReadLine();

    }

    private static void PrintStats(List<SessInfo> Sessions)
    {
      Console.WriteLine("Current active sessions:");
      Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
      foreach (SessInfo sessInfo in Sessions)
      {
        if (!dictionary1.ContainsKey(sessInfo.BotSignature))
        {
          dictionary1.Add(sessInfo.BotSignature, 1);
        }
        else
        {
          Dictionary<string, int> dictionary2;
          string botSignature;
          (dictionary2 = dictionary1)[botSignature = sessInfo.BotSignature] = dictionary2[botSignature] + 1;
        }
      }
      if (dictionary1.Count > 0)
      {
        foreach (KeyValuePair<string, int> keyValuePair in dictionary1)
          Console.WriteLine(string.Format("'{0}': {1} user {2}", (object) keyValuePair.Key, (object) keyValuePair.Value, keyValuePair.Value > 1 ? (object) "S" : (object) ""));
      }
      else
        Console.WriteLine("There is no active sessions");
    }
  }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题而且我已经弄明白了。

我的问题(也许是你的问题)是我在不同的端点使用相同的基本端口。

请注意,它适用于Windows,但不适用于Linux:

var host = new ServiceHost(typeof(MyService), new Uri("net.tcp://localhost:10500/UCB"));
host.AddServiceEndpoint(typeof(IService1), CreateTcpBinding(), "IService1");
host.AddServiceEndpoint(typeof(IService2), CreateTcpBinding(), "IService2");

这将创建以下内容:

  • net.tcp://localhost:10500/UCB/IService1
  • net.tcp://localhost:10500/UCB/IService2

它适用于Windows,但不适用于Linux,与端口共享有关。

为了在Linux上工作,我们需要有不同的端口,例如:

  • net.tcp://localhost:10500/UCB/IService1
  • net.tcp://localhost:10501/UCB/IService2

工作代码:

var host = new ServiceHost(typeof(MyService), new Uri("net.tcp://localhost"));
host.AddServiceEndpoint(typeof(IService1), CreateTcpBinding(), "net.tcp://localhost:10500/UCB/IService1");
host.AddServiceEndpoint(typeof(IService2), CreateTcpBinding(), "net.tcp://localhost:10501/UCB/IService2");