【问题标题】:Execute code only if parent transaction completes仅在父事务完成时执行代码
【发布时间】:2011-10-04 14:34:19
【问题描述】:

下面的代码会输出:

Event published
Command executed

使用 System.Transactions 如何让 EventPublisher 在父事务中登记,以便它仅在 CommandHandler 完成时执行。因此,输出将是:

Command executed
Event published

代码:

class Program
    {
        static void Main(string[] args)
        {           
            var handler = new CommandHandler();
            handler.Execute();

            Console.ReadLine();
        }
    }

    public class CommandHandler
    {
        public void Execute()
        {
            var foo = new Foo();
            foo.DoSomething();

            Console.WriteLine("Command executed");
        }
    }

    public class EventPublisher
    {
        public void PublishEvent()
        {
            Console.WriteLine("Event published");
        }
    }
public class Foo
{
    public void DoSomething()
    {
        new EventPublisher().PublishEvent();
    }
}

【问题讨论】:

    标签: .net transactions system.transactions


    【解决方案1】:

    我认为你需要的是除了跨越所有代码的事务之外的异步代码执行

    【讨论】:

      【解决方案2】:

      解决方案实际上是通过实现IEnlistmentNotification来参与事务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-29
        • 2012-02-08
        • 2018-01-12
        相关资源
        最近更新 更多