0
votes

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.

1
Can you share the code that you're executing at the time of application startup? Please update your question with code.Gaurav Mantri
Thanks! What's the PartitionKey/RowKey for the entity you're trying to insert? I don't see that in your code. It would also help if you can include the code for LogEntity as well.Gaurav Mantri
the partitionKey key contains the instance id and machine name, so should give me different entries for different instancesdaxu
the partitionKey key contains the instance id and machine name, so should give me different entries for different instances ==> This sounds right.Gaurav Mantri
will be interested to see if you get different results, or it is just me.daxu

1 Answers

7
votes

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.