daxu
daxu

Reputation: 4074

azure web app multiple instances confusion

I have an web app which has 2 instances as default and from resource explorer, I can see there are two instances. However, in global.asax code I have this code:

public class LogEntity : TableEntity
    {
        public LogEntity(string partitionKey, string rowKey)
        {
            this.PartitionKey = partitionKey;
            this.RowKey = rowKey;
        }
        public LogEntity() { }
        public string Submitter { get; set; }
    }

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        Random ran = new Random();
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
        var tableClient = storageAccount.CreateCloudTableClient();
        CloudTable table = tableClient.GetTableReference("logs");
        table.CreateIfNotExists();

        var key1 = ran.Next();
        var machineName1 = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");

        LogEntity log1 = new LogEntity(Environment.MachineName + ":" + machineName1 + ":" + key1.ToString(), "instance started");
        TableBatchOperation batchOperation1 = new TableBatchOperation();
        batchOperation1.Insert(log1);
        table.ExecuteBatch(batchOperation1);

    }
}

However, from my azure table log, I can only see one log entry generated, and it is always from the same instance.

Does this mean when I have multiple instances, only one instance will call application_start? I thought all instances should hit application_start as they run independently. However, my log seems contradict my understanding.

UPDATE

I showed the code that logs to azure table. My expection is that as there are two instances, I should see two log entries created in the azure table. However, there is always just one entry.

Upvotes: 0

Views: 2770

Answers (1)

daxu
daxu

Reputation: 4074

I had some discussion with Microsoft support and understands why now.

For my site, I got ARR Affinity on, with that setting on, azure load balancer will only enable the ones that have been accessed (as Affinity cookie). As a result, I only see my code run once.

When I disable Affinity, azure load balancer will enable all instances at once and I see my code run on all instances.

Upvotes: 8

Related Questions