没有足够的存储空间可用于“Console.ReadLine”。

Not enough storage is available for `Console.ReadLine`.`

我正在使用双重 service/console 模型来测试我的一项服务。聚光灯下的代码是:

static void Main(string[] args)
{
    // Seems important to use the same service instance, regardless of debug or runtime.
    var service = new HostService();
    service.EventLog.EntryWritten += EventLogEntryWritten;

    if (Environment.UserInteractive)
    {
        service.OnStart(args);
        Console.WriteLine("Host Service is running. Press any key to terminate.");
        Console.ReadLine();
        service.OnStop();
    }
    else
    {
        var servicesToRun = new ServiceBase[] { service };
        Run(servicesToRun);
    }
}

当我 运行 应用程序在调试器下使用 F5 时,在行 Console.ReadLine(); 上我得到一个 System.IO.IOException,"Not enough storage is available to process this command."

ReadLine的唯一目的是等到有人按下一个键结束应用程序,所以我无法想象需要这么多存储的数据来自哪里。

这是一项服务,其输出可能设置为 Windows 应用程序,将输出更改为控制台应用程序,这应该会消失。

我遇到了同样的问题,我在项目属性下找到了设置,但我正在创建一个 windows 应用程序,所以我无法更改应用程序类型。

这是我使用的代码。

Dim t As Task = New Task(AddressOf DownloadPageAsync)
t.Start()
Console.WriteLine("Downloading page...")
Console.ReadLine()

异步子 DownloadPageAsync()

Using client As HttpClient = New HttpClient()
    Using response As HttpResponseMessage = Await client.GetAsync(page)
        Using content As HttpContent = response.Content
            ' Get contents of page as a String.
            Dim result As String = Await content.ReadAsStringAsync()
            ' If data exists, print a substring.
            If result IsNot Nothing And result.Length > 50 Then
                Console.WriteLine(result.Substring(0, 50) + "...")
            End If
        End Using
    End Using
End Using

结束子