承接上集 Azure Table Service 效能調優,本文解決三大開發實戰問題:PartitionKey/RowKey 設計原則、Web Role 中自訂 HttpHandler 正確設定、Role 設定檔 Storage 連線字串初始化錯誤修復。
Azure 表格儲存以 PartitionKey + RowKey 複合主鍵分散資料。設計原則:
DateTime.Now.Ticks 導致寫入集中最新分區)string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks)) 支援時間範圍掃描參考:More about PartitionKey & RowKey
<system.web>
<httpHandlers>
<add verb="*" path="*.do" type="MyHandler" />
</httpHandlers>
</system.web>
部署至 Azure Web Role (IIS 7+ 整合模式) 會報錯:HTTP 500.21 - Handler not found in integrated pipeline
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="KeywordsHandler" path="*.do" verb="GET"
type="KeywordsWebRole.KeywordsHandler, KeyWordsWebRole"
resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
關鍵點:system.webServer/handlers、runAllManagedModulesForAllRequests="true"、validateIntegratedModeConfiguration="false"
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
// Exception: SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used.
public override bool OnStart()
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
RoleEnvironment.Changed += (sender, arg) =>
{
if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
.Any(change => change.ConfigurationSettingName == configName))
{
if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
{
RoleEnvironment.RequestRecycle();
}
}
};
});
return base.OnStart();
}
原理:Azure SDK 1.3+ 要求發布者模式 讀取組態設定,支援執行時動態變更不重啟角色。
Azure 開發三大實戰坑:PartitionKey/RowKey 設計決定查詢效能與成本、IIS 7 整合管線強制 system.webServer/handlers、組態讀取需發布者模式。掌握這三點,避開 80% 部署與效能問題。Azure 平台專業性極強,建議配合官方最佳實踐文檔與 SDK 更新持續學習。