Windows Azure 作為微軟基於雲端運算的作業系統核心,承載著「軟體即服務」的技術願景。在實際開發部署過程中,開發者常會遇到各類技術挑戰。本文彙總專業人士實戰經驗,系統整理 Azure 表格服務優化、WebRole 自訂 HttpHandler、Storage 連線設定等高頻問題及解決方案,助您繞過坑點、提升開發效率。
預設連線數限制可能成為瓶頸,透過組態檔或程式碼提升上限:
<!-- 組態檔設定 -->
<system.net>
<connectionManagement>
<add address="*" maxconnection="24" />
</connectionManagement>
</system.net>
<!-- 程式碼設定 -->
ServicePointManager.DefaultConnectionLimit = 24;
減少額外網路交互,提升請求響應速度:
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>
ServicePointManager.Expect100Continue = false;
純查詢操作時關閉追蹤可節省記憶體與 CPU:
context.MergeOption = MergeOption.NoTracking;
合理規劃分區鍵與列鍵是表格儲存效能的核心,詳見官方文件《More about "PartitionKey" & "RowKey" in windows azure table storage》。
部署後的 WebRole 實際運行於 IIS 7 整合管線模式,傳統 <system.web><httpHandlers> 設定會報錯。正確做法是在 <system.webServer> 節點配置:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add path="*.do" name="KeywordsHandler" verb="GET"
type="KeywordsWebRole.KeywordsHandler,KeywordsWebRole"
resourceType="Unspecified" allowPathInfo="true"/>
</handlers>
</system.webServer>
直接呼叫 CloudStorageAccount.FromConfigurationSetting("DataConnectionString") 會拋出異常: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();
}
}
};
});
掌握這些核心技巧,能讓您在 Azure 開發之路上少走彎路。雖然雲端平台技術門檻較高,但多了解這些實戰細節,總能在關鍵時刻派上用場。