以下代码将输出:
Event published
Command executed
使用系统。我如何让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();
}
}
我认为您需要的是跨所有代码的事务之外的异步代码执行
解决方案实际上是通过实现IEnlistmentNotification在事务中登记。