Windows Azure. Get unique application instance ID - asp.net

We use "Websites" in Windows Azure. I need to get unique application instance. I know about "RoleEnvironment.CurrentRoleInstance.Id" but as long as I know it works only for web roles. So I'm thinking about the following way to get unique application ID for each instance. I'm interested whether it's OK to use. Or am I missing something?
Once the application is started I generate some unique name (in the "Application_Start" method). For example, Guid.NewGuid().
Once it's done I store this unique value in some static variable.
When a second instance in started, steps 1 and 2 are repeated and some other new unique name is generated
This way I have unique names for each instance. Of course, once a site (or some instance) is restarted I'll get new absolutely unique values. But it's fine in our case.
Please let me know if you find any pitfalls in this solution

You could use the COMPUTERNAME or the WEBSITE_INSTANCE_ID environment variables instead.
In fact, you can browse the full list of environment variables at https://<yoursite>.scm.azurewebsites.net/Env

This should work, but of course the GUID will only be around for the lifetime of the w3wp process. So you would have unique names per w3wp process which would imply that you would have unique names for each instance.
If you wanted to use a more targeted unique name, you could experiment with the ARR affinity cookie. Azure Web app runs with a sticky session load balancer (when running with multiple instances) by default. It injects ARR affinity cookie in the HTTP response that helps in redirecting a user to the same instance with which it established the session at the first time.
You can also use the environment variables to uniquely identify the instance, i.e. Environment.ComputerName.

Related

Can multiple requests affect users in one single IIS instance?

I'm having a problem on my application. It's an ASP.NET application set up on IIS 10.
Let's say one system page is accessible by 20 users. The page works perfectly (no logical error on coding) every action works and delivers the expected values requested by users.
The problem is, whenever someone requests let's say, the same method as another user at the same time (with different values), the application randomly throws an error to one of these users. We've checked for log errors and all of them are system index out of range errors, which never happened in our QA server.
I randomly thought about testing that exact scenario (adding different values with another user at the same time) and I saw it happen for the first time on the QA server. We've managed to reproduce the error multiple times.
While we don't discard the possibility that this could be another issue, did anyone else experience something like that?
The question is: Can IIS manage the same requests, multiple times at the same time within the same instance without any trouble? Does it run on multiple threads or something like that?
Thanks for taking time for answering this, if you need any info
Stick to your question
Yes IIS can handle very easily (more efficient as well)
As per your application concern without code I can't point out but you may consider few points
Is it happening for just one method or for all. If it happening for just one that means you are trying to use such a code that may used by another user
You are using such a array or list which is null or empty for other user. Like a user has First Name Followed by Last Name But other user don't fill last name and you are using that last name property
May be u r using HttpContext and trying to use same as for different users
May be You are using types which are not Thread safe
So these can be possible cases but without code we can't assume.
About your problem, for multiple requests from different user, iis will create a thread in the application pool for each request. For multiple requests from the same user, it will only run in one thread and affect only the user's instance. Unless the instance or resource is a shared resource and your code does not perform any lock operations.
IIS, including most web servers, use threads to process requests, so multiple requests will be executed in parallel unless you place a lock. A web server usually has a minimum and a maximum number of work programs. These work programs are adjusted according to the CPU or memory of the current hardware. If resources are exhausted, new requests will be queued until new resources are available.
So what you need to do may be to modify the application code to take multi-threading and synchronization into consideration.

Online users count

How is possible to count all application on-line "Sessions"?
Not all connected PC (from ID address), but all sessions.
E.g.: If in same PC open Chrome and Firefox with my app, the counter must be 2.
If you have only one instance of the application, it is easier. You can create a middleware and store session ids in a Set. Then to get the count, you can just use the element count in the Set. If you have multiple instances of the application, you need to create a common db like Redis, and use their Set features. Don't forget to remove the session ids when the session ends, or perhaps make them expire after a while unless renewed.

Session and Cookies asp.net

I was reading an article on asp.net session vs viewstate and after reading the article I got a doubt i.e. the article says if I create a session variable i.e.
Session("username")="username" this value is saved on webserver and webserver on start of first request creates a cookie and store it on a client machine and then retrieve the session variables for specific user based on the cookie value stored on client machine.
My doubts are - How and where does the session variables are stored on the server.
My understanding is based on the sessionID i.e. cookie value stored on client machine the webserver created a folder and for all the session variables created it create file and store the data i.e. For all the session variables stored on webserver there will be only one cookie stored on client machine- Please correct me if I am wrong in understanding to what I mentioned above.
ASP.Net allows you to configure where you want your sessions variables stored. The documentation on the different options you can use is available here:
https://msdn.microsoft.com/en-us/library/ms178586%28v=vs.140%29.aspx
The short version is that session variables are stored in memory with your application by default, but you can also use a separate process or even Sql Server. None of the options uses a folder on your disk, though. That would be slow. If you really want to use a folder on the server, you have to write your own custom provider.

Static variables in asp.net?

In Web application, I am using static variable, when more than one user accessing the same page where static variables declare, it could be conflict variables?, for example i am storing the session value in static variable, if the two employee are accessing the same page at a time it could wrong process na?.
Yes, there can be conflicts. You can end up with a lot of unexpected behaviors. Avoid them as you may have concurrency issues.
Yes conflicts will likely occur, the state of the variable is shared across the entire app domain.
A static variable has only one value for each appDomain. It means the same will be shared by all the request in the application . This may lead to conflicts . So never user static variable until and unless you are very sure about the behavior , use session instead.
Yes, that's why you should be VERY careful when you use static variables in a web app. You will run in concurrency issues as more than one thread servicing a request can modify the value of the variable.Static Variables Scope is Application Level. If you store some thing in it, then its a wrong thing. If one user saves the data, Same time another user accessed the same page then he will also get the same data. So try to store the values in Sessions.
While this works in single-user environment, What happens in a multi-user environment, if there are 2 users simultaneously logged in from two computers, User 1 sets the value as 200, then User 2 sets the value as 400. after that user 1 invokes the Get Value button. What will he see as the value?

Multiple domains, same application but different result on each subdomain. how to keep track of which account for the subdomain?

I have a web application where users can register. Each user will get a subdomain to their account. and each subdomain will be kind of unique but use the same web application as every one else.
The application is .net and i run it on a iis 2003 or 2008 (not desided yet.) and I am aiming for about 1000 users. ( 1000 subdomains)
For example:
user1.mydomain.com
user2.mydomain.com
user3.mydomain.com alias sub.something.com
it is not always possible to get the account ID from the subdomainname as you see bacause of the aliases of the domains.
so I was thinking about different approaches.
1: for every page request a database lookup is performeed and the Id of the account is returned. drawbacks? extra database communication. And the table containing all the domainnames for all the users, e text search in the database every time a page is requested does not sound as a good idea.
2: as number 1, but accountId is saved as session. but what happens with 1 thousend accounts, and every account has 1000 viewers, large amount of memory is used??
3:I dont know if this one is possible, but i think its a good one...
I was thinking about creating all the subdomains..
User1., user2., user3....
And the alias for each subdomain is saved within IIS.
Within each folder for all the domains, i create an ID file. just containing the ID of the user as:
id.aspx
-----
accountID = ##;
-----
And after that i want all the files for the application to be virtual included into each subdomain.
Explanation:
Each subdomain contains the following files:
id.aspx
file1.aspx virtual file from web.mydomain.com
file2.aspx virtual file from web.mydomain.com
File3.aspx virtual file from web.mydomain.com
....
and file1...3 is somewhere else on the IIS.
Is this possible?
So please, is there someone here that could help me with this problem?
What should I do?
I Guess this is a common "problem" that many persons have solved in a good whay.
Thanks for your time reading this.
Best Regards
Matte
To me, #3 can be challenge from management perspective. I will rather go with approach similar to #2 - your mappings are stored in database. When user logs in, look up is performed and the account id is cached in session state. If you are already using session state for something else then adding this bit information is not really a overhead. Otherwise, you can consider other options for caching this information - for example, it could be put into the cookie (of course you have to add mechanism such as encryption/check-sum to avoid tampering). You can also make account id as part of user identity (by pushing it into authentication cookie which is anyway made tamper-proof by ASP.NET runtime).
Considering that you have 1000 users, I will most probably choose to build in-memory look up table (store the dictionary in ASp.NET cache or static variables), considering 10 aliases per user, it will not have more than 10000 entries - build the table at the time of application start and start of each request, do a look-up and get the account id and store it into HttpContext so that it's available hence forth.

Resources