If you call
t.RegisterChanges();
instead of calling RegisterTaskDefnition on the last row, do you get the same error?