0

下面的代码将输出:

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();
    }
}
4

2 回答 2

1

I think what you need is the asynchronous code execution beside the transactions that spans all the code

于 2011-10-04T15:01:59.950 回答
0

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

于 2011-10-06T17:30:05.063 回答