导出 (0) 打印
全部展开

RoleEnvironment 类

Provides information about the configuration, endpoints, and status of running role instances.

命名空间: Microsoft.WindowsAzure.ServiceRuntime
程序集: Microsoft.WindowsAzure.ServiceRuntime(在 microsoft.windowsazure.serviceruntime.dll 中)

用法

public static class RoleEnvironment
public final class RoleEnvironment
public final class RoleEnvironment

You use the RoleEnvironment class to obtain configuration settings, local storage resources, and endpoint information for a specified role instance. The class also provides events that allow the role instance to react to configuration changes and receive notification when the role instance is about to be stopped.

The following code example shows how to get and write out the value for a configuration setting called MySetting by using the GetConfigurationSettingValue method:


var settingValue = RoleEnvironment.GetConfigurationSettingValue("MySetting"); 
Trace.WriteLine("The setting value is: " + settingValue, "Information");

For more information about defining and configuring settings, see Windows Azure Service Configuration Schema and Windows Azure Service Definition Schema.

The following code example shows how to retrieve a local storage resource and write a text file to it:


// Retrieve an object that points to the local storage resource
LocalResource localResource = RoleEnvironment.GetLocalResource("localStoreTwo");

//Define the file name and path
string[] paths = { localResource.RootPath, "MyStorageTest.txt"};
String filePath = Path.Combine(paths);

using (FileStream writeStream = File.Create(filePath))
{
   Byte[] textToWrite = new UTF8Encoding(true).GetBytes("Testing Web role storage");
   writeStream.Write(textToWrite, 0, textToWrite.Length);
}

The following code example shows how to retrieve endpoint information for the current role instance:


var roleInstance = RoleEnvironment.CurrentRoleInstance; 
foreach (RoleInstanceEndpoint instanceEndpoint in roleInstance.InstanceEndpoints.Values) 
{  
   Trace.WriteLine("Instance endpoint address and port: " + instanceEndpoint.IPEndpoint, "Information");
   Trace.WriteLine("Protocol for the endpoint: " + , instanceEndpoint.Protocol, "Information");
}

The following code example shows how to retrieve the configuration changes that have been made to a role instance:


public override bool OnStart()
{
   RoleEnvironment.Changed += RoleEnvironmentChanged;
  
   return base.OnStart();
}
   
private void RoleEnvironmentChanged(object sender, RoleEnvironmentChangedEventArgs e)
{
   // Get the list of configuration changes
   var settingChanges = e.Changes.OfType<RoleEnvironmentConfigurationSettingChange>();
      
   foreach (var settingChange in settingChanges) 
   {
      var message = "Setting: " + settingChange.ConfigurationSettingName;
      Trace.WriteLine(message, "Information");
   }
}

The events are only raised if an internal endpoint is defined for the role in the ServiceDefinition.csdef file. For more information about defining endpoints, see How to Define Internal Endpoints for a Role.


System.Object
  Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment

此类型的任何公共静态(在 Visual Basic 中为 Shared)成员都是线程安全的。但不保证所有实例成员都是线程安全的。

开发平台

Windows XP Home Edition, Windows XP Professional, Windows Server 2003、Windows Server 2008 和 Windows 2000

目标平台

社区附加资源

Microsoft 正在进行一项网上调查,以了解您对 MSDN 网站的意见。 如果您选择参加,我们将会在您离开 MSDN 网站时向您显示该网上调查。

是否要参加?
显示:
© 2014 Microsoft