【问题标题】:TextWriter closed unexpectedly after async call in Azure webjob在 Azure webjob 中的异步调用后 TextWriter 意外关闭
【发布时间】:2016-03-09 11:34:16
【问题描述】:

我创建了一个简单的 Azure WebJob,它调用异步任务并尝试使用 TextWriter 记录一些信息。 在调用异步任务之前写入日志很好,但在调用之后TextWriter 被关闭。在下面的示例代码中,我只是调用Task.Delay() 进行演示。

如果我将await log.WriteLineAsync("") 调用更改为log.WriteLine("") 也没关系

public class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        JobHostConfiguration config = new JobHostConfiguration();
        JobHost host = new JobHost(config);
        host.Call(typeof (Program).GetMethod("DoJobNow"), new { value = "Hello world!" });
        host.RunAndBlock();
    }


    [NoAutomaticTrigger]
    public async static void DoJobNow(string value, TextWriter log)
    {
        await log.WriteLineAsync("Write with textwriter");
        await log.WriteLineAsync("Write with textwriter again - still open");
        await Task.Delay(100);
        await log.WriteLineAsync("TextWriter is closed?? Exception here!");

    }
}

当我在本地运行此代码时,我在最后一次日志调用中得到一个System.ObjectDisposedException,如果我在Task.Delay 行上发表评论,它工作正常! 这是为什么呢?

【问题讨论】:

标签: c# async-await azure-webjobs


【解决方案1】:

您需要设置 DoJobNow 的返回类型才能返回一个任务。调用线程会导致TextWriter被释放

[NoAutomaticTrigger]
public async static Task DoJobNow(string value, TextWriter log)
{
    await log.WriteLineAsync("Write with textwriter");
    await log.WriteLineAsync("Write with textwriter again - still open");
    await Task.Delay(100);
    await log.WriteLineAsync("TextWriter is closed?? Exception here!");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2021-07-20
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多