以下代码将输出:
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();
}
}
答案 0 :(得分:1)
我认为您需要的是跨越所有代码的事务旁边的异步代码执行
答案 1 :(得分:0)
解决方案实际上是通过实施IEnlistmentNotification来登记交易。