Mengekspos pengaturan konfigurasi peran sebagai variabel lingkungan dengan XPath
Penting
Cloud Services (klasik) sekarang tidak lagi digunakan untuk pelanggan baru dan akan dihentikan untuk semua pelanggan pada 31 Agustus 2024. Penyebaran baru sebaiknya menggunakan Azure Resource Manager yang baru berdasarkan model penyebaran Azure Cloud Services (dukungan tambahan) .
Dalam file definisi layanan cloud atau layanan peran web, Anda dapat mengekspos nilai konfigurasi runtime sebagai variabel lingkungan. Nilai XPath berikut didukung (yang sesuai dengan nilai API).
Nilai XPath ini juga tersedia melalui pustaka Microsoft.WindowsAzure.ServiceRuntime.
Aplikasi berjalan di emulator
Menunjukkan bahwa aplikasi berjalan di emulator.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/Deployment/@emulated" |
Kode | var x = RoleEnvironment.IsEmulated; |
Deployment ID
Mengambil ID penyebaran untuk instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/Deployment/@id" |
Kode | var deploymentId = RoleEnvironment.DeploymentId; |
ID Peran
Mengambil ID peran saat ini untuk instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/@id" |
Kode | var id = RoleEnvironment.CurrentRoleInstance.Id; |
Perbarui Domain
Mengambil domain pembaruan instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/@updateDomain" |
Kode | var ud = RoleEnvironment.CurrentRoleInstance.UpdateDomain; |
Fault domain
Mengambil domain pembaruan kesalahan instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/@faultDomain" |
Kode | var fd = RoleEnvironment.CurrentRoleInstance.FaultDomain; |
Nama peran
Mengambil nama peran instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/@roleName" |
Kode | var rname = RoleEnvironment.CurrentRoleInstance.Role.Name; |
Pengaturan konfigurasi
Mengambil nilai dari pengaturan konfigurasi yang ditentukan.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/ConfigurationSettings/ConfigurationSetting[@name='Setting1']/@value" |
Kode | var setting = RoleEnvironment.GetConfigurationSettingValue("Setting1"); |
Jalur penyimpanan lokal
Mengambil jalur penyimpanan lokal instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@path" |
Kode | var localResourcePath = RoleEnvironment.GetLocalResource("LocalStore1").RootPath; |
Ukuran penyimpanan lokal
Mengambil ukuran penyimpanan lokal untuk instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='LocalStore1']/@sizeInMB" |
Kode | var localResourceSizeInMB = RoleEnvironment.GetLocalResource("LocalStore1").MaximumSizeInMegabytes; |
Protokol titik akhir
Mengambil protokol titik akhir untuk instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@protocol" |
Kode | var prot = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].Protocol; |
IP Titik Akhir
Mendapatkan alamat IP titik akhir yang ditentukan.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@address" |
Kode | var address = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Address |
Port titik akhir
Mengambil port titik akhir untuk instans.
Jenis | Contoh |
---|---|
XPath | xpath="/RoleEnvironment/CurrentInstance/Endpoints/Endpoint[@name='Endpoint1']/@port" |
Kode | var port = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint.Port; |
Contoh
Berikut contoh peran pekerja yang membuat tugas startup dengan variabel lingkungan bernama TestIsEmulated
yang diatur ke nilai @emulated xpath.
<WorkerRole name="Role1">
<ConfigurationSettings>
<Setting name="Setting1" />
</ConfigurationSettings>
<LocalResources>
<LocalStorage name="LocalStore1" sizeInMB="1024"/>
</LocalResources>
<Endpoints>
<InternalEndpoint name="Endpoint1" protocol="tcp" />
</Endpoints>
<Startup>
<Task commandLine="example.cmd inputParm">
<Environment>
<Variable name="TestConstant" value="Constant"/>
<Variable name="TestEmptyValue" value=""/>
<Variable name="TestIsEmulated">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated"/>
</Variable>
...
</Environment>
</Task>
</Startup>
<Runtime>
<Environment>
<Variable name="TestConstant" value="Constant"/>
<Variable name="TestEmptyValue" value=""/>
<Variable name="TestIsEmulated">
<RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated"/>
</Variable>
...
</Environment>
</Runtime>
...
</WorkerRole>
Langkah berikutnya
Pelajari lebih lanjut tentang file ServiceConfiguration.cscfg.
Buat paket ServicePackage.cspkg.
Fungsikan desktop jarak jauh untuk peran.