7

我正在使用 Web API 将大文件流式传输到客户端,但我想记录下载是否成功。也就是说,如果服务器发送了文件的全部内容。

HttpResponseMessage完成发送数据后,是否有某种方法可以获取回调或事件?

也许是这样的:

var stream = GetMyStream();
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");    
// This doesn't exist, but it illustrates what I'm trying to do.
response.OnComplete(context =>
{
    if (context.Success)
        Log.Info("File downloaded successfully.");
    else
        Log.Warn("File download was terminated by client.");
});
4

3 回答 3

5

编辑:我现在已经使用真实连接(通过提琴手)对此进行了测试。

我继承StreamContent并添加了自己的OnComplete操作来检查异常:

public class StreamContentWithCompletion : StreamContent
{
    public StreamContentWithCompletion(Stream stream) : base (stream) { }
    public StreamContentWithCompletion(Stream stream, Action<Exception> onComplete) : base(stream) 
    { 
        this.OnComplete = onComplete; 
    }

    public Action<Exception> OnComplete { get; set; }

    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        var t = base.SerializeToStreamAsync(stream, context);
        t.ContinueWith(x =>
        {
            if (this.OnComplete != null)
            {
                // The task will be in a faulted state if something went wrong. 
                // I observed the following exception when I aborted the fiddler session: 
                // 'System.Web.HttpException (0x800704CD): The remote host closed the connection.'
                if (x.IsFaulted)
                    this.OnComplete(x.Exception.GetBaseException());
                else
                    this.OnComplete(null);
            }
        }, TaskContinuationOptions.ExecuteSynchronously);
        return t;
    }
}

然后我像这样使用它:

var stream = GetMyStream();
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContentWithCompletion(stream, ex =>
{
    if (ex == null)
        Log.Info("File downloaded successfully.");
    else
        Log.Warn("File download was terminated by client.");
});
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");    
return response;
于 2012-12-20T02:29:16.217 回答
3

我不确定是否有直接信号表明一切正常,但是您可以使用一个技巧来找出连接在您结束之前以及在您完全发送文件之后是否存在。

例如,Response.IsClientConnected如果客户端仍然连接,则返回 true,因此您可以检查以下内容:

// send the file, make a flush
Response.Flush();
// and now the file is fully sended check if the client is still connected
if(Response.IsClientConnected)
{
  // log that all looks ok until the last byte.
}
else
{
  // the client is not connected, so maybe have lost some data
}

// and now close the connection.
Response.End();
于 2012-12-20T00:50:14.563 回答
1

如果服务器发送了文件的全部内容

其实没什么可做的:)

这听起来可能非常简单,但您会知道是否引发了异常 - 如果您关心服务器交付而不是客户端取消中途。IsClientConnected基于 ASP.NETHttpResponse而不是 WebApi。

于 2012-12-20T09:54:50.307 回答