将来的に私のウェブサイトがクラウド上でホストされ、多くの要求を処理できるようにしたいと考えています。ASP.NET MVC静的変数の安全性
静的変数はどれくらい安全ですか?
別々のユーザーによる別々のリクエストが実際にこれらの静的変数を共有しているため、それらは安全ではありませんか?それとも、スレッド/シャーディングなどのサイトを広げて(高負荷を処理するために)スレッドが静的変数を共有しているからですか?
主に静的プロパティを持つヘルパークラスがありますが、代わりに各クラスのインスタンスを作成してインスタンスにアクセスするようにこのアーキテクチャを変更する必要がありますか?
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()
{
}
}
}
同じルールが適用されます...スタティックはアプリドメイン内で共有されます。あなたの例のような共有設定データの場合、これはあなたが望むものと同じかもしれませんが、一度しか初期化しないよう注意してください。 –