我想確保我的網站能夠在將來託管在雲上,並且它可以處理很多請求。ASP.NET MVC靜態變量有多安全
靜態變量有多安全?
它們是不安全的,因爲單獨用戶的不同請求實際上共享這些靜態變量?或者是因爲如果你通過線程/分片或類似的方式傳播站點(處理高負載),這些線程共享靜態變量?
主要是我有助手類,具有靜態屬性,我應該改變這個架構,以便我創建每個類的實例並訪問實例嗎?
E.G下面是我在做什麼的樣本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mvc.Mailer;
namespace MVCWebsite.Helpers
{
public class AppSettings
{
public static void OnAppInit()
{
//General
AppName = "MyApp";
DesktopBaseURLs = new Dictionary<string, string>();
DesktopBaseURLs.Add("dev", "localhost:50560");
DesktopBaseURLs.Add("test", "www.test.whatever.com");
DesktopBaseURLs.Add("live", "www.whatever.com");
MobileBaseURLs = new Dictionary<string, string>();
MobileBaseURLs.Add("dev", "m.local.whatever.com");
MobileBaseURLs.Add("test", "m.test.whatever.com");
MobileBaseURLs.Add("live", "m.whatever.com");
//Emails
EmailHostName = AppName + ".com"; //For the moment atleast
NoReplyEmailAddress = "[email protected]" + EmailHostName.ToLower();
SupportEmailAddress = "[email protected]" + EmailHostName.ToLower();
ErrorEmailAddress = "[email protected]" + EmailHostName.ToLower();
//Resources
TempFileURL = "/content/temp/";
UserDataURL = "/content/user-content/";
ProfilePicturesURL = UserDataURL + "profile-pictures/";
var a = GlobalHelper.GetURLAsServerPath(ProfilePicturesURL);
var b = a;
}
//General
public static string AppName { get; set; }
public static Dictionary<string, string> DesktopBaseURLs;
public static Dictionary<string, string> MobileBaseURLs;
//Emails
public static string EmailHostName { get; set; }
public static string NoReplyEmailAddress { get; set; }
public static string SupportEmailAddress { get; set; }
public static string ErrorEmailAddress { get; set; }
//Resources
public static string UserDataURL { get; set; }
public static string TempFileURL { get; set; }
public static string ProfilePicturesURL { get; set; }
//Methods
public static void SetAppURL()
{
}
}
}
同樣的規則適用...靜態在應用程序域內共享。對於共享的配置數據(如你的例子),這可能正是你想要的,但要注意只從一個線程初始化一次。 –