共用方式為


Creating a Custom Action

Retired Content

This content and the technology described is outdated and is no longer being maintained. For more information, see Transient Fault Handling.

patterns & practices Developer Center

On this page:
Deserializing Custom Actions | Defining a Custom Action | Using Your Custom Action

The Autoscaling Application Block defines the scale action type for reactive rules that allows you to specify a target and a scale amount. You can also define custom actions for reactive rules; for example, to send an email notification or to run a SQL Azure script to modify a database. This topic outlines the steps you must take to create and configure a custom action.

For more information, see section "Implementing a Custom Action" in Chapter 5, "Making Tailspin Surveys More Elastic" of the Developer's Guide.

After you have created and configured a custom action, administrators must be able to use the action when they are creating or editing autoscaling rules. Depending on your environment, administrators might create and edit rules in the default rules XML file, in a custom format in a custom rules store, or through a custom UI.

You package a custom action in an assembly that you deploy with the Autoscaling Application Block. The assembly must contain code that can deserialize your custom action from your rules store and return an implementation of the ReactiveRuleAction class.

The following sections describe the three steps to implement and use a custom action:

  • Deserializing Custom Actions. Adding support for the block's configuration infrastructure.
  • Defining a Custom Action. Creating the runtime behavior for the action.
  • Using Your Custom Action. Modifying your autoscaling rules to use the custom action.

Deserializing Custom Actions

The following snippet shows how the AutoscalingRules.xsd file defines the actions element of a reactive rule. Notice how the schema allows you to use an alternative element in a different namespace in place of the scale element.

<xs:element name="actions" minOccurs="0" maxOccurs="1">
  <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="scale" type="ScaleActionType"/>
      <xs:any processContents="lax" namespace="##other"/>
    </xs:choice>
  </xs:complexType>
  <xs:unique name="ScaleActionTargetUnique">
    <xs:selector xpath="r:scale"/>
    <xs:field xpath="@target"/>
  </xs:unique>
</xs:element>

In the assembly that defines your custom action, you must provide code that can deserialize the content of your custom element.

Note

You can use the schema for your custom namespace to provide validation and IntelliSense functionality in the XML editor that you use to edit your autoscaling rules.

The following code snippet shows how you should deserialize the content of your custom action element. Your deserialization class must extend the ReactiveRuleActionElement class and return a ReactiveRuleAction instance from the CreateAction method.

[XmlRoot(ElementName = "customAction", Namespace = "http://custom_namespace")]
public class CustomAction : ReactiveRuleActionElement
{
    public override ReactiveRuleAction CreateAction()
    {
        ...
    }
}

If you are not using the default XML rules store, you must provide code that will deserialize a rule from your custom store.

Defining a Custom Action

To define a custom action, you must extend the abstract ReactiveRuleAction class. This class defines a single method shown below.

public abstract class ReactiveRuleAction
{
    public abstract RuleEvaluationResult GetResult(ReactiveRule forRule);
}

If your custom action makes a change to your application's run-time configuration, your GetResult method should return an instance derived from the ConfigurationChangeResult class. For other actions, your GetResult method should return an instance derived from the ExecuteActionResult class. Use the Execute method to define the custom action that you want to perform.

namespace Microsoft.Practices.EnterpriseLibrary.WindowsAzure.Autoscaling.Rules
{
    public abstract class ExecuteActionResult : RuleEvaluationResult
    {
        protected ExecuteActionResult(Rule sourceRule)
            : base(sourceRule)
        {
        }

        public abstract string Description { get; }

        public abstract void Execute(IRuleEvaluationContext context);
    }
}

The Execute method of the derived class should only throw exceptions of type ActionExecutionException. Any other type of exception will be treated as critical and will not be handled.

Using Your Custom Action

After you have created the assembly with all of the necessary classes to define your custom action, you must configure the Autoscaling Application Block to load the assembly when the block starts. You can do this using the Enterprise Library configuration tool. For information about using the Enterprise Library configuration tool to configure the Autoscaling Application Block, see the topic "Entering Configuration Information."

The following screenshot shows how to enter the name of an assembly that implements a custom action in the configuration tool.

Hh680921.9843B910826DE410193E20F4D880F3E5(en-us,PandP.50).png

Adding the custom action

You must include the assembly when you deploy your application to Microsoft Azure.

Next Topic | Previous Topic | Home

Last built: June 7, 2012