网络API - 如何检测时的响应已完成发送 [英] Web Api - How to detect when a response has finished being sent

查看:131
本文介绍了网络API - 如何检测时的响应已完成发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个Web API方法,我产生一个文件,然后将其分流到像这样

In a web api method I am generating a file and then streaming it to the response like so

public async Task<HttpResponseMessage> GetFile() {
    FileInfo file = generateFile();
    var msg = Request.CreateResponse(HttpStatusCode.OK);

    msg.Content = new StreamContent(file.OpenRead());
    msg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    msg.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {FileName = file.Name};

    return msg;
}

由于这是一个生成的文件我想删除它的反应已完成流,但我似乎无法找到在管道这个钩子了。

because this a generated file I want to delete it after the response has finished streaming but I can't seem to find a hook in the pipeline for this.

我想我可以把一个参考文件中的静态和设置,拉出来的值同样的静态变量,并删除自定义的MessageHandler。然而,这似乎像它不可能是正确的都使用静态的(当这应该都是每个请求),也因为我不得不注册一个独立的路线。

I suppose that I can put a reference to the file in a static and set up a custom MessageHandler that pulls values out of this same static variable and deletes. However, this seems like it can't possibly be right both because of the use of a static (when this should all be per-request) and because I'd have to register a separate route.

我见过的<一个href=\"http://stackoverflow.com/questions/16359485/how-to-know-when-a-response-has-finished-being-sent-to-the-client\">this问题但它似乎不是真的有很多有用的反应。

I've seen this question but it seems to not really have much of a useful response.

推荐答案

尼斯的场景!...用消息处理程序的问题是,响应写发生在主机层和下面的消息处理层,所以他们并不理想。 ..

Nice scenario!...the problem with using message handlers is that response writing happens at the host layers and below message handlers layer, so they are not ideal...

以下是你如何能做到这一点的例子:

Following is an example of how you could do it:

msg.Content = new CustomStreamContent(generatedFilePath);


public class CustomStreamContent : StreamContent
{
    string filePath;

    public CustomStreamContent(string filePath)
        : this(File.OpenRead(filePath))
    {
        this.filePath = filePath;
    }

    private CustomStreamContent(Stream fileStream)
        : base(content: fileStream)
    {
    }

    protected override void Dispose(bool disposing)
    {
        //close the file stream
        base.Dispose(disposing);

        try
        {
            File.Delete(this.filePath);
        }
        catch (Exception ex)
        {
            //log this exception somewhere so that you know something bad happened
        }
    }
}

顺便说一句,你是因为你把一些数据转换成PDF生成此文件。如果是的话,那么我想你可以使用 PushStreamContent 为此而转换后的数据直接写入响应流。这样,您不必首先生成一个文件,然后担心以后将其删除。

By the way, are you generating this file because you are converting some data into PDF. If yes, then I think you could use PushStreamContent for this purpose by directly writing the converted data into the response stream. This way you need not generate a file first and then worry about deleting it later.

这篇关于网络API - 如何检测时的响应已完成发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆