开发者

Azure Table Storage OnStart Constructor Query

开发者 https://www.devze.com 2023-04-10 06:34 出处:网络
I\'ve been looking at some Azure samples and doing some general searching around Table Storage.I\'ve noticed a bit of a pattern using OnStart and a static constructor.For example the following type o开

I've been looking at some Azure samples and doing some general searching around Table Storage. I've noticed a bit of a pattern using OnStart and a static constructor. For example the following type o开发者_如何学Cf code is found in both locations:

// Get connection string and table name from settings.
connectionString = RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString");
tableName = RoleEnvironment.GetConfigurationSettingValue("TableName");

// Reference storage account from connection string. 
storageAccount = CloudStorageAccount.Parse(connectionString);

// Create Table service client.
tableClient = storageAccount.CreateCloudTableClient();

My question is why on both locations? Surely this is just duplication? The static constructor will be called once we start working with the data type, while OnStart will run when during application start-up.

Personally I think the static constructor makes more sense.

I just want to make sure I'm understanding things correctly,

Mark


In the example you shared, I can find two places where there's similar code. One is in OnStart (in the RoleEntryPoint), and one is in a static constructor in a class called DataLayer. DataLayer appears to be used in the web application (running under IIS), so a different class in a different process from the RoleEntryPoint.

The one in RoleEntryPoint appears to be initializing storage (creating the table) before the application starts up. The one in DataLayer seems to be initializing some variables to avoid code repetition in the other methods (parsing the connection string, instantiating the client).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号