Global.asax.cs 和静态变量
在 WCF 服务中,我需要创建一个可以随时随地访问的变量。我的服务的所有方法都需要访问该数据,并且我只能加载一次。所以我考虑在 Global.asax.cs 中使用静态变量。但是我不确定该变量的范围是什么。该数据将用于所有请求吗?我的理解是应该这样做,因为应该在整个应用程序域中使用相同的静态变量。这是正确的吗?
public static IList<MyData> Data { get; set; }
private static IList<MyData> Load()
{
return Big data struct from DB.
}
protected void Application_Start(object sender, EventArgs e)
{
Data = Load();
}
最后,有没有更好的方法来实现这一目标?我不太喜欢静态变量......
In a WCF Service, I need to create a variable which should be accessible anytime from anywhere. All methods of my service need to have access to that data and I can load it only once. So I though about using a static variable in the Global.asax.cs. However I am not sure to understand what is going to be the scope of that variable. Is that data is going to be used for all the requests? My understanding is that it should because the same static variable should be used across the App Domain. Is that correct?
public static IList<MyData> Data { get; set; }
private static IList<MyData> Load()
{
return Big data struct from DB.
}
protected void Application_Start(object sender, EventArgs e)
{
Data = Load();
}
Finally, is there a better way to achieve that? I am not a big fan of static variable...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用应用程序变量:
在实际实现中并没有太大不同,只是现在可以通过该变量名称作为应用程序变量在全局范围内使用它。
You could use an application variable:
Not really much different in actual implementation except that now it's available globally through that variable name as an application variable.
是的,静态变量对于应用程序中的所有线程/会话都是有效/可见的。
AFAIK,静态变量不在AppDomains之间共享。要完成此任务,您可以查看 在这个例子中。
您不需要全局变量,但您想要任何人、任何地方都可以访问的东西,您看到矛盾了吗?任何类型的单例都只是一个全局变量。
但就你的情况而言,这似乎是最好的解决方案。您只需确保您的全局对象是不可变并且线程安全。
Yes a static variable is valid / visible from all threads / sessions in your application.
AFAIK, static variables are not shared between AppDomains. For accomplishing this task you may have a look at this example.
You do not want a global variable but you want something that is accessible from anybody and everywhere, you see the contradiction? Any kind of singleton is just a global variable.
But in your case it seem to be the best solution. You just should make sure that your global object is immutable and thread safe.
我将使用单例模式来存储您的“应用程序范围”变量。它是静态的,将在第一次使用后分配,并且在应用程序的生命周期内可用。我还认为这比使用像应用程序这样的非类型化哈希表要好得多。对我来说,应用程序存储是 ASP 的遗物,在面向对象的世界中不再有用。
请注意,静态变量仅初始化一次,因为每个 Web 请求/服务调用都在其自己的线程中运行。
像这样,您可以在第一次使用时加载数据,并使用 MyData.Data 从任何地方访问它:
事件最好是在静态构造函数中初始化,因为这样调用将是线程安全的。
I would use the Singleton pattern to store your "application wide" variable. It is static, will be allocated after the first usage and is available for the lifetime of your application. I also think this is a lot better than using an untyped HashTable like Application. To me Application storage is a relict from ASP and no longer useful in the object oriented world.
Be careful that the static variable is initialized only once, because every web request/service call runs in its own thread.
Like this you could load the data on the first usage and access it from everywhere with MyData.Data:
Event better would be an initialisation in the static constructor because then the call would be thread-safe.