BVernon
BVernon

Reputation: 3747

Are static variables shared between same app accessed by different host names?

I'm already fairly certain the answer is no but can't seem to find a definitive answer. If I deploy an application that can be accessed by multiple host names, each host name will spin up its own separate instance of the application and static variables will not be shared between them, right?

EDIT: The hosting environment is IIS and there is only one website that has multiple bindings to different host names.

Upvotes: 0

Views: 752

Answers (1)

Conrad
Conrad

Reputation: 118

IIS allows you to set the number of instances your website can actually run on. You will be able to access your variables if IIS is configured to run only a single instance and multiple domains are pointed to this single instance. Because of resource management, IIS will restart these instances when needed and oftenly. This is why if a request comes in and finds that IIS has already restarted the instance, it will have already lost the contents of the previous static variable values. However, if the same instance is found, the static variables will still mantain their values.

It is not recommended to use static variables for cross request message handling because most hosting providers do not use a single instance to run your website but may use a web farm to do so. This further leads us to why it is not good to use In-proc sessions when handling sessions. [Because the session values will be lost once the instance is restarted].

As of web farms, many instances are started to handle multiple incoming requests. So, one request may be handled by another instance different from the instance that handled the previous request. The new instance handling the request will definately not have the values of the variables of the other instance. Even if they are copies of the same website. This is because they are recognized as two different applications running on the server at the same time and the operating system does not permit direct sharing of the static variables accross the two different applications.

You can however make the best use of server side application settings in IIS or use cookies to route information accross multiple domains. These will enable you to for-example handle single login from a single website for all the other websites of your system. You can achieve this when the authentication website sets cookies for which can be accessed by the other websites. ASP.NET allows you to do such.

Thank you

Upvotes: 1

Related Questions