简体   繁体   English

ASP.NET静态对象行为

[英]Asp.net static object behaviour

I have the following class as part of an asp.net application. 我将以下课程作为asp.net应用程序的一部分。

public sealed class SomeClass    
 {
    private static string appId = Guid.NewGuid().ToString();

    public static ReadSomethingFromDb(){}

    public static WriteSomethingToDb(){}
}

There are more than one application instances in the same application pool, and they all access the same database. 同一应用程序池中有多个应用程序实例,并且它们都访问同一数据库。 I want the operations performed by the above class to be uniquely tied to the instance that performed it, hence the use of the appId. 我希望由上述类执行的操作唯一地绑定到执行该操作的实例,因此要使用appId。 So adding a record to the database would for example contain a name, address and appId. 因此,将记录添加到数据库将例如包含名称,地址和appId。 This has been simplified for discussion purposes. 出于讨论目的,已对此进行了简化。

Assuming that I have two instances running at mysite.a and mysite.b the above class would generate two different guids. 假设我有两个实例在mysite.a和mysite.b上运行,则上述类将生成两个不同的向导。

My problem is that mysite.a sometimes produces more than one guid, which is unexpected. 我的问题是mysite.a有时会产生多个GUID,这是意外的。

Thank you in advance 先感谢您

That will get a new Guid per AppDomain ; 这将为每个AppDomain获取一个新的Guid you'll have new AppDomain s whenever: 每当出现以下情况,您将拥有新的AppDomain

  • the app-pool recycles 应用程序池回收
  • different machines 不同的机器
  • you reboot the server 您重新启动服务器
  • you edit a file such as web.config 您编辑诸如web.config之类的文件
  • IIS just feels like spawning another one IIS 就像生成另一个

In short, this isn't a very robust way of proving that one app is the same (although it indicate different applications clearly). 简而言之,这不是证明一个应用程序相同的非常健壮的方法(尽管它清楚地表明了不同的应用程序)。

Why not just something like the site path or IIS name as an identifier? 为什么不只将站点路径或IIS名称之类的内容用作标识符?

A new AppDomain is created under various circumstances. 在各种情况下都会创建一个新的AppDomain。 For instance, when the web.config is changed. 例如,当web.config更改时。 At that time, no new requests are scheduled for the original AppDomain; 当时,没有为原始AppDomain安排新的请求; and all new requests are sent to the new one. 并将所有新请求发送到新请求。

During the transition, both AppDomains will still be running. 在过渡期间,两个AppDomain仍将运行。

after Marc's comment i ended up using this at application start which seems to work 在Marc发表评论后,我最终在应用程序启动时使用了它,这似乎可行

string applicationId = Environment.MachineName+""
    +System.Web.HttpContext.Current.Request.ApplicationPath;

this covered me as there is a chance of a multi machine setup plus the application instances which depends on the path. 这涵盖了我,因为有可能会设置多台计算机以及取决于路径的应用程序实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM