但是,我不明白为什么当我的业务逻辑抛出我的自定义异常之一时,以下代码为什么起作用UnprocessableException:
try
{
await next.Invoke(context);
}
catch (UnprocessableException uex)
{
Logger.Warn(uex);
context.Response.StatusCode = 422;
var responseContent = JsonConvert.SerializeObject(new { uex.Message });
await context.Response.WriteAsync(responseContent);
}
// more specific exceptions resulting in HTTP 4xx status
Run Code Online (Sandbox Code Playgroud)
但是当完全意外IndexOutOfRangeException的事情被catch链中的最后一个块抓住时
catch (Exception ex)
{
Logger.Error(ex);
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var responseContent = env.IsDevelopment()
? JsonConvert.SerializeObject(new { ex.Message, ex.StackTrace })
: JsonConvert.SerializeObject(new { Message = "An internal error occured" });
await context.Response.WriteAsync(responseContent);
}
Run Code Online (Sandbox Code Playgroud)
尝试设置状态码时抛出此异常:
System.InvalidOperationException: StatusCode cannot be set, …Run Code Online (Sandbox Code Playgroud) 我将文件存储在blob存储帐户中的一个容器中.我需要在第二个容器中创建一个zip文件,其中包含第一个容器中的文件.
我有一个使用worker角色和DotNetZip工作的解决方案,但因为zip文件最终可能是1GB大小我担心在进程中使用MemoryStream对象等进行所有工作并不是最好的方法.我最关心的是内存使用和释放资源,因为这个过程可能每天发生几次.
下面是一些非常简化的代码,显示了worker角色的基本过程:
using (ZipFile zipFile = new ZipFile())
{
foreach (var uri in uriCollection)
{
var blob = new CloudBlob(uri);
byte[] fileBytes = blob.DownloadByteArray();
using (var fileStream = new MemoryStream(fileBytes))
{
fileStream.Seek(0, SeekOrigin.Begin);
byte[] bytes = CryptoHelp.EncryptAsBytes(fileStream, "password", null);
zipFile.AddEntry("entry name", bytes);
}
}
using (var zipStream = new MemoryStream())
{
zipFile.Save(zipStream);
zipStream.Seek(0, SeekOrigin.Begin);
var blobRef = ContainerDirectory.GetBlobReference("output uri");
blobRef.UploadFromStream(zipStream);
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以提出更好的方法吗?
我正在使用此解决方案下载文件,压缩并发送给用户,(Azure blobs).
这是我到目前为止所提出的:
public void DownloadImages(DownloadImagesModel model)
{
if (ModelState.IsValid && model.ImageIDs != null && model.ImageIDs.Count > 0)
{
var currentUser = _claimService.GetCurrentClaimsIdentity(TimeZoneExtensions.GetCurrentDate());
var images = _fileService.GetImages(currentUser.CompanyID, model.ImageIDs).ToList();
if (images != null && images.Count > 0)
{
string imageContainerURL = _fileService.GetImageContainerURL(currentUser.CompanyID) + currentUser.ImageContainerToken;
using (var zipOutputStream = new ZipOutputStream(Response.OutputStream))
{
zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
Response.BufferOutput = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + "zipFileName.rar");
Response.ContentType = "application/octet-stream";
foreach (var image in images) …Run Code Online (Sandbox Code Playgroud)