我目前正在运行一个专用服务器的站点,但希望将来使用 Microsoft 的 Azure 云平台进行扩展,但我不确定我的代码的某些部分是否可以在 azure 上运行。
该站点由一个项目库组成,每个项目都有一个图像。图像存储在 sqlserver 数据库中。
我创建了一个 http 处理程序,它将图像缓存到磁盘并将请求重定向到图像(如帖子末尾所示)。
这些图像被保存到我的 ASP.NET 应用程序中一个名为“imagecache”的虚拟目录中。(即 ~/imagecache/ )。
由于 Web 应用程序将在 Azure 平台上的许多虚拟机实例中运行,因此需要在实例之间共享图像,对吗?
所以我的问题真的是......实现我已经拥有的与天蓝色兼容的最佳方法是什么?
谢谢
图像生成代码..
public class getimage : IHttpHandler {
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void ProcessRequest (HttpContext context) {
try
{
string uniqueid = "";
if(context.Request.QueryString["id"]) uniqueid = context.Request.QueryString["id"].ToString();
string dir = "~/imagecache/";
string image_target_path = dir + "image-file-" + uniqueid + ".png";
byte[] data = null;
context.Response.ContentType = "image/png";
if (!System.IO.File.Exists(context.Server.MapPath(image_target_path)))
{
if (context.Request.QueryString["id"] != null)
{
// get image data from the database
data = ImageHelper.getDatabaseImageDataByID(Convert.ToInt32(uniqueid));
using (System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(data)))
{
// save image to disk in the virtual dir
img.Save(context.Server.MapPath(image_target_path));
}
}
else
{
// set a sample image if no id is set
image_target_path = "~/images/noimage.png";
}
}
// redirect request to image file
context.Response.Redirect(image_target_path, false);
}
catch (Exception ex)
{
log.Error(ex.Message, ex);
}
}
public bool IsReusable {
get {
return false;
}
}
}