Catch远程主机关闭连接c#

时间:2017-01-21 13:44:09

标签: c# asp.net model-view-controller ashx

我在捕获错误时遇到问题

  

远程主机关闭了连接

在生产服务器上但它正在我的本地计算机上运行。我的代码就像这样

try
{
    SqlDataSource_Gift.saveGiftDownloadLogs(guestGuid, guestEmail, couponCode, albumId, trackId, "Downloading");
    List<faeTrackInfoDetail> result = SqlDataSource_Track.getTrackInfoDetail(trackId);
            faeTrackInfoDetail item = result.First();
    catalogueId = item.CatalogueID;
    filename = item.DiscNo.ToString() + "." + item.TrackNo.ToString() + " - " + CleanFileName(item.TrackTitle) + "." + mediaType;
    urlPath = "mydomain.com/Download/DownloadAudioTrack.ashx?id=" + catalogueId + "&trackid=" + trackId + "&ext=" + mediaType;
    context.Response.ClearContent();
    context.Response.ClearHeaders();
    if (mediaType != "mp3")
    {
       context.Response.ContentType = "application/octet-stream";
    }
    else
    {
       context.Response.ContentType = "application/mpeg";
    }
    context.Response.AppendHeader("Content-Disposition", "inline; filename=" + (char)(34) + filename + (char)(34));
    context.Response.BufferOutput = false;
    using (var wc = new WebClient())
    {
        using (var download = wc.OpenRead(urlPath))
        {
            using (var respStream = context.Response.OutputStream)
            {
                download.CopyTo(respStream);

                SqlDataSource_Gift.saveGiftDownloadLogs(guestGuid, guestEmail, couponCode, albumId, trackId, "Downloaded");
            }
        }
    }
}
catch (Exception ex)
{
    string exceptionMessage = ex.Message;
    if (exceptionMessage.ToLower().Contains("the remote host closed the connection."))
    {
        // Download Cancelled!
        SqlDataSource_Gift.saveGiftDownloadLogs(guestGuid, guestEmail, couponCode, albumId, trackId, "Cancelled");
    }
    else
    {
        // Download Error!
        SqlDataSource_Gift.saveGiftDownloadLogs(guestGuid, guestEmail, couponCode, albumId, trackId, "Error");
    }
}

我需要知道用户何时取消下载并将其记录到数据库中。现在,当代码部署到prod服务器但在我调试时工作时它不起作用。

我需要检查IIS中的设置吗?提前谢谢。

0 个答案:

没有答案