Compensator 类

定义

表示所有补偿资源管理器 (CRM) 补偿器的基类。

C#
public class Compensator : System.EnterpriseServices.ServicedComponent
继承

示例

下面的代码示例演示了此类的用法。

C#
// A CRM Compensator
public class AccountCompensator : Compensator
{

    private bool receivedPrepareRecord = false;

    public override void BeginPrepare ()
    {
        // nothing to do
    }

    public override bool PrepareRecord (LogRecord log)
    {

        // Check the validity of the record.
        if (log == null) return(true);
        Object[] record = log.Record as Object[];
        if (record == null) return(true);
        if (record.Length != 2) return(true);

        // The record is valid.
        receivedPrepareRecord = true;
        return(false);
    }

    public override bool EndPrepare ()
    {
        // Allow the transaction to proceed onlyif we have received a prepare record.
        if (receivedPrepareRecord)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }

    public override void BeginCommit (bool commit)
    {
        // nothing to do
    }

    public override bool CommitRecord (LogRecord log)
    {
        // nothing to do
        return(false);
    }

    public override void EndCommit ()
    {
        // nothing to do
    }

    public override void BeginAbort (bool abort)
    {
        // nothing to do
    }

    public override bool AbortRecord (LogRecord log)
    {

        // Check the validity of the record.
        if (log == null) return(true);
        Object[] record = log.Record as Object[];
        if (record == null) return(true);
        if (record.Length != 2) return(true);

        // Extract old account data from the record.
        string filename = (string) record[0];
        int balance = (int) record[1];

        // Restore the old state of the account.
        AccountManager.WriteAccountBalance(filename, balance);

        return(false);
    }

    public override void EndAbort ()
    {
        // nothing to do
    }
}

此补偿器由以下辅助角色类使用。

C#
// A CRM Worker
[Transaction]
public class Account : ServicedComponent
{

    // A data member for the account file name.
    private string filename;

    public string Filename
    {
        get
        {
            return(filename);
        }
        set
        {
            filename = value;
        }
    }

    // A boolean data member that determines whether to commit or abort the transaction.
    private bool commit;

    public bool AllowCommit
    {
        get
        {
            return(commit);
        }
        set
        {
            commit = value;
        }
    }

    // Debit the account,
    public void DebitAccount (int ammount)
    {

        // Create a new clerk using the AccountCompensator class.
        Clerk clerk = new Clerk(typeof(AccountCompensator),
          "An account transaction compensator", CompensatorOptions.AllPhases);

        // Create a record of previous account status, and deliver it to the clerk.
        int balance = AccountManager.ReadAccountBalance(filename);

    Object[] record = new Object[2];
    record[0] = filename;
        record[1] = balance;

        clerk.WriteLogRecord(record);
        clerk.ForceLog();

        // Perform the transaction
        balance -= ammount;
        AccountManager.WriteAccountBalance(filename, balance);

        // Commit or abort the transaction
        if (commit)
        {
            ContextUtil.SetComplete();
        }
        else
        {
            ContextUtil.SetAbort();
        }
    }
}

下面的代码示例演示了一个客户端,该客户端用于执行此补偿程序和辅助角色。

C#
using System;

public class CrmClient
{

    public static void Main ()
    {

        // Create a new account object. The object is created in a COM+ server application.
        Account account = new Account();

        // Transactionally debit the account.
        try
        {
            account.Filename = System.IO.Path.GetFullPath("JohnDoe");
            account.AllowCommit = true;
            account.DebitAccount(3);
        }
        finally
        {
            account.Dispose();
        }
    }
}

注解

用户应从此对象扩展,以便编写自定义事务补偿器。

补偿器必须始终具有公共构造函数;否则,恢复引擎无法创建它。

有关详细信息,请参阅如何:Create补偿资源管理器 (CRM)

构造函数

Compensator()

初始化 Compensator 类的新实例。

属性

Clerk

获取一个值,该值表示补偿资源管理器 (CRM) Clerk 对象。

方法

AbortRecord(LogRecord)

在中断阶段期间将日志记录传递到补偿资源管理器 (CRM) 补偿器。

Activate()

当从池创建或分配对象时,由基础结构调用。 重写此方法以将自定义初始化代码添加到对象中。

(继承自 ServicedComponent)
BeginAbort(Boolean)

通知补偿资源管理器 (CRM) 补偿器事务完成的中断阶段,并且通知即将传递记录。

BeginCommit(Boolean)

通知补偿资源管理器 (CRM) 补偿器事务完成的提交阶段,并且通知即将传递记录。

BeginPrepare()

通知补偿资源管理器 (CRM) 补偿器事务完成的准备阶段,并且通知即将传递记录。

CanBePooled()

在将对象放回到池中之前结构调用该方法。 重写此方法以决定是否将对象放回到池中。

(继承自 ServicedComponent)
CommitRecord(LogRecord)

在提交阶段期间以前向顺序传递日志记录。

Construct(String)

恰好在调用构造函数后由基础结构调用,并且在构造函数字符串中传递。 重写该方法以使用结构字符串值。

(继承自 ServicedComponent)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Deactivate()

当对象即将停用时由基础结构调用。 重写该方法以在使用实时 (JIT) 编译代码或对象池时向对象添加自定义终止代码。

(继承自 ServicedComponent)
Dispose()

释放由 ServicedComponent 使用的所有资源。

(继承自 ServicedComponent)
Dispose(Boolean)

释放由 ServicedComponent 占用的非托管资源,还可以另外再释放托管资源。

(继承自 ServicedComponent)
EndAbort()

通知补偿资源管理器 (CRM) 补偿器在中断阶段期间已接收所有可用日志记录。

EndCommit()

通知补偿资源管理器 (CRM) 补偿器在提交阶段期间已传递所有可用日志记录。

EndPrepare()

通知补偿资源管理器 (CRM) 补偿器在准备阶段期间已具有所有可用日志记录。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
PrepareRecord(LogRecord)

在准备阶段期间以前向顺序传递日志记录。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IRemoteDispatch.RemoteDispatchAutoDone(String)

此 API 支持产品基础结构,不能在代码中直接使用。

确保在 COM+ 上下文中,ServicedComponent 类对象的 done 位在远程方法调用后设置为 true

(继承自 ServicedComponent)
IRemoteDispatch.RemoteDispatchNotAutoDone(String)

不确保在 COM+ 上下文中,ServicedComponent 类对象的 done 位在远程方法调用后设置为 true

(继承自 ServicedComponent)
IServicedComponentInfo.GetComponentInfo(Int32, String[])

获取关于 ServicedComponent 类实例的某些信息。

(继承自 ServicedComponent)

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1