【发布时间】:2020-09-27 21:46:23
【问题描述】:
我有一个使用 ASP.Net MVC 5 开发的 Web 应用程序,托管在 Azure 中。我使用的是共享应用服务,而不是虚拟机。最近 Azure 开始显示警告,我需要减少我的应用程序对工作人员临时文件的使用。
重启应用后,问题就消失了。似乎通过重新启动清除了临时应用程序。
如何检测和防止临时文件使用量的意外增长。我不确定是什么生成了 20 GB 的临时文件。我应该寻找什么来减少临时应用程序的使用?我没有在代码中的临时文件中明确存储任何内容,数据存储在数据库中,所以不确定要查找什么?
为了使临时文件的使用保持健康状态并防止任何意外增长,应遵循哪些最佳做法?
注意:我的 Web 应用中有多个具有相同物理路径的虚拟路径。
try
{
if (file != null && file.ContentLength > 0)
{
var fileName = uniqefilename;
CloudStorageAccount storageAccount = AzureBlobStorageModel.GetConnectionString();
if (storageAccount != null)
{
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
string containerName = "storagecontainer";
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
bool isContainerCreated = container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);
CloudBlobDirectory folder = container.GetDirectoryReference("employee");
CloudBlockBlob blockBlob = folder.GetBlockBlobReference(fileName);
UploadDirectory = String.Format("~/upload/{0}/", "blobfloder");
physicalPath = HttpContext.Server.MapPath(UploadDirectory + fileName);
file.SaveAs(physicalPath);
isValid = IsFileValid(ext, physicalPath);
if (isValid)
{
using (var fileStream = System.IO.File.OpenRead(physicalPath))
{
blockBlob.Properties.ContentType = file.ContentType;
blockBlob.UploadFromFile(physicalPath);
if (blockBlob.Properties.Length >= 0)
{
docURL = blockBlob.SnapshotQualifiedUri.ToString();
IsExternalStorage = true;
System.Threading.Tasks.Task T = new System.Threading.Tasks.Task(() => deletefile(physicalPath));
T.Start();
}
}
}
}
}
}
catch (Exception ex)
{
}
//Delete File
public void deletefile(string filepath)
{
try
{
if (!string.IsNullOrWhiteSpace(filepath))
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(filepath);
}
}
catch(Exception e) { }
}
【问题讨论】:
-
如果您将代码作为代码块插入到文本中而不是插入图像链接,那么使用源代码会更容易:stackoverflow.com/help/how-to-ask
-
@@longestwayround 我添加了代码。
-
我更新了我的答案。我认为您的问题是删除调用在完成之前退出。
-
如果回答帮助您解决了问题,请采纳为回答;如果没有,更多信息可能会帮助您从其他用户那里获得更多建议。
标签: azure asp.net-mvc-5 azure-web-app-service temporary-files