Chcę zapewnić, że moja strona internetowa będzie mogła być hostowana w chmurze w przyszłości, a także, że może obsłużyć wiele żądań.ASP.NET MVC Jak bezpieczne są zmienne statyczne?
Jak bezpieczne są zmienne statyczne?
Czy są one niebezpieczne, ponieważ oddzielne żądania od oddzielnych użytkowników faktycznie udostępniają te statyczne zmienne? A może dlatego, że jeśli przenosisz stronę na wątki/sharding lub podobne, (aby poradzić sobie z dużymi obciążeniami), wątki dzielą się statycznymi zmiennymi?
Głównie Mam klasy pomocnicze z właściwościami statycznymi, czy powinienem zmienić tę architekturę, aby zamiast tego utworzyć instancję każdej klasy i uzyskać dostęp do instancji?
np Oto próbka tego, co robię:
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()
{
}
}
}
Obowiązują te same zasady ... statyczny jest udostępniany w domenie aplikacji. W przypadku współdzielonych danych konfiguracji (jak w twoim przykładzie) może to być dokładnie to, czego potrzebujesz, ale staraj się tylko zainicjować tylko jeden raz i z jednego wątku. –