Asp.net Static Object Behavior

I have the following class as part of an asp.net application.

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

    public static ReadSomethingFromDb(){}

    public static WriteSomethingToDb(){}
}

      

There are multiple instances of an application in the same application pool, and they all have access to the same database. I want the operations performed by the above class to be uniquely bound to the instance that was executing it, hence the use of appId. So adding a record to the database, for example, must contain the name, address and appId. This has been simplified for discussion.

Assuming I have two instances running in mysite.a and mysite.b, the above class will generate two different outlines.

My problem is that mysite.a sometimes produces more than one guid, which is unexpected.

Thank you in advance

+2


a source to share


3 answers


This will get a new one Guid

for AppDomain

; you will have a new AppDomain

one every time:

  • app-pool recycles utility
  • different cars
  • restart the server
  • you are editing a file like web.config
  • IIS just feels like another is spawning.


In short, this is not a very reliable way of proving that one application is the same (although it clearly indicates different applications).

Why not just something like the site path or IIS name as an identifier?

+3


a source


A new AppDomain is created under various circumstances. For example, when changing web.config. No new requests were scheduled for the original AppDomain at that time; and all new requests are sent to a new one.



During the transition, both AppDomains will work.

+2


a source


after Marc's comment i ended up using this when starting the app which seems to work

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

      

this covered me as there is a possibility of multiple machines and application instances being installed that depend on the path.

+1


a source







All Articles