Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TcpListener.AcceptSocket()在Windows服务中失败_C#_Sockets_Windows Services_Tcplistener - Fatal编程技术网

C# TcpListener.AcceptSocket()在Windows服务中失败

C# TcpListener.AcceptSocket()在Windows服务中失败,c#,sockets,windows-services,tcplistener,C#,Sockets,Windows Services,Tcplistener,我有一个在调试中或作为独立Windows应用程序正确执行的以下代码的实例: TcpListener tcpListener = new TcpListener(IPAddress.Any, 4554); tcpListener.Start(); while (true) { try { using (Socket socket = tcpListener.AcceptSocket()) { // Code here is

我有一个在调试中或作为独立Windows应用程序正确执行的以下代码的实例:

TcpListener tcpListener = new TcpListener(IPAddress.Any, 4554);
tcpListener.Start();

while (true)
{
    try
    {
        using (Socket socket = tcpListener.AcceptSocket())
        {
            // Code here is reached in Debug or as a Console Application
            // but not as a Windows Service
        }
    }
    catch (SocketException se)
    {
        // This is never reached
    }
    catch (Exception ex)
    {
        // This is never reached
    }
    finally
    {
        // This is never reached in the Windows Service
    }
}
但是,当我将其作为Windows服务安装时,它会在
tcpListener.AcceptSocket()
上崩溃,并将以下内容记录到事件查看器中:

MyService.exe[768]中发生未经处理的异常('System.Net.Sockets.SocketException')。即时调试此异常失败,出现以下错误:不支持尝试的操作

即使尝试捕捉异常,我也无法记录更多内容。在调试中单步执行代码不会产生任何效果,因为代码成功地阻止了应用程序并等待客户端连接

有没有一种方法可以为Windows服务实现这一点?

的建议(和)让我发现了代码中的一个bug。ServiceBase类包含以下内容:

protected override void OnStart(string[] args)
{
    _worker = new Thread(ExecuteService);
    _worker.Start();
}

private void ExecuteService()
{
    for (;;)
    {
         if (_stop.WaitOne(1000))
        {
            new TcpServer().StartTcpServer();
            return;
        }
    }
}
正确的实现是删除
for
循环,该循环正在重新实例化侦听器。以下是最终代码:

protected override void OnStart(string[] args)
{
    _worker = new Thread(ExecuteService);
    _worker.Start();
}

private static void ExecuteService()
{
    new TcpServer().StartTcpServer();
}

很可能是
Start
引发了
SocketException
。因为它不在你的尝试中,你的捕获和最终不会被击中。将
Start
移动到try中,并在catch语句中检查
SocketException
的ErrorCode属性。这会让你更好地了解问题所在。然后对照错误代码查看确切的问题是什么。熟悉如何调试服务。这是可能的。@vcsjones我试过了,结果也一样。我还在
tcpListener.AcceptSocket()
之前和之后添加了日志记录,它在调用之前成功记录了日志,但在调用之后没有。谢谢@usr。我按照你的建议做了,发现我的服务逻辑正在重新实例化这个类。