- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 552字
- 2021-04-02 18:51:55
Using SetConfigurationSettingPublisher()
The CloudStorageAccount
class in the Windows Azure Storage Client library encapsulates a StorageCredential
instance that can be used to authenticate against the Windows Azure Storage Service. It also exposes a FromConfigurationSetting()
factory method that creates a CloudStorageAccount
instance from a configuration setting.
This method has caused much confusion since, without additional configuration, it throws an InvalidOperationException
with a message of "SetConfigurationSettingPublisher
needs to be called before FromConfigurationSetting()
can be used." Consequently, before using FromConfigurationSetting()
, it is necessary to invoke SetConfigurationSettingPublisher()
once. The intent of this method is that it can be used to specify alternate ways of retrieving the data connection string that FromConfigurationSetting()
uses to initialize the CloudStorageAccount
instance. This setting is process-wide, so is typically done in the OnStart()
method of the RoleEntryPoint
class for the role.
The following is a simple implementation for SetConfigurationSettingPublisher()
:
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); });
There are several levels of indirection here, but the central feature is the use of a method that takes a String
parameter specifying the name of the configuration setting and returns the value of that setting. In the example here, the method used is RoleEnvironment.GetConfigurationSettingValue()
. The configuration-setting publisher can be set to retrieve configuration settings from any location including app.config
or web.config
.
The use of SetConfigurationSettingPublisher()
is no longer encouraged. Instead, it is better to use CloudStorageAccount.Parse()
, which takes a data connection string in canonical form and creates a CloudStorageAccount
instance from it. We see how to do this in the Connecting to the Windows Azure Storage Service recipe.
In this recipe, we will learn how to set and use a configuration-setting publisher to retrieve the data connection string from a configuration file.
How to do it...
We are going to add an implementation for SetConfigurationSettingPublisher()
to a worker role. We do this as follows:
- Create a new cloud project.
- Add a worker role to the project.
- Add the following to the
WorkerRole
section of theServiceDefinition.csdef
file:<ConfigurationSettings> <Setting name="DataConnectionString" /> </ConfigurationSettings>
- Add the following to the
ConfigurationSettings
section of theServiceConfiguration.cscfg
file:<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName={ACCOUNT_NAME};AccountKey={ACCOUNT_KEY}"/>
- Replace
WorkerRole.Run()
with the following:public override void Run() { UseFromConfigurationSetting("{CONTAINER_NAME}"); while (true) { Thread.Sleep(10000); Trace.WriteLine("Working", "Information"); } }
- Replace
WorkerRole.OnStart()
with the following:public override bool OnStart() { ServicePointManager.DefaultConnectionLimit = 12; CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) => { configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); return base.OnStart(); }
- Add the following method, implicitly using the configuration setting publisher, to the
WorkerRole
class:private void UseFromConfigurationSetting(String containerName) { CloudStorageAccount cloudStorageAccount =CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); CloudBlobClient cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.Create(); }
How it works...
In steps 1 and 2, we set up the project.
In steps 3 and 4, we define and provide a value for the DataConnectionString
setting in the service definition and service configuration files. We must replace {ACCOUNT_NAME}
and {ACCOUNT_KEY}
with appropriate values for the account name and access key.
In step 5, we modify the Run()
method to invoke a method that accesses the storage service. We must provide an appropriate value for {CONTAINER_NAME}
.
In step 6, we modify the OnStart()
method to set a configuration setting publisher for the role instance. We set it to retrieve configuration settings from the service configuration file.
In step 7, we invoke CloudStorageAccount.FromConfigurationSetting()
, which uses the configuration setting publisher we added in step 6. We then use the CloudStorageAccount
instance to create CloudBlobClient
and CloudBlobContainer
instances that we use to create a new container in blob storage.
- Joomla! 1.5 Site Blueprints
- Maya影視動畫高級模型制作全解析
- 攝影照片修飾完全自學手冊
- SolidWorks2014基礎實例教程
- 板繪教室:SAI零基礎日系動漫插畫入門教程
- Photoshop CC入門與進階
- Photoshop CC UI設計標準培訓教程
- RAW 數碼底片演義
- 音樂制作7天速成:Cubase編曲教程
- AutoCAD 2022中文版從入門到精通(標準版)
- Origin科技繪圖與數據分析
- Moodle Course Conversion: Beginner's Guide
- Python 3 Web Development Beginner's Guide
- Learning Ext JS
- jQuery 1.3 with PHP