Union leader. Muckraking journalist. Legislator. Presidential candidate. And now, assassin’s victim.

Fernando Villavicencio, who was gunned down at a rally on Wednesday, had a long history in Ecuadorean public affairs, largely as an antagonist to those in power. He rose to prominence as a union leader at the state oil company, Petroecuador, and later played a crucial role in exposing a corruption scandal involving the administration of former President Rafael Correa.

Mr. Correa, a socialist, was Ecuador’s longest-serving democratically elected president, leading the nation for a decade, through 2017. A commodities boom helped him lift millions out of poverty, but his authoritarian style and the corruption allegations that trailed him deeply divided the country.

And Mr. Villavicencio was “always contesting the power” of Mr. Correa, according to Caroline Ávila, an Ecuadorean political analyst.

As a journalist, Mr. Villavicencio obtained documents about a government surveillance program that he sent to WikiLeaks but eventually published himself. Some of his work led to death threats and charges that were widely criticized as politically motivated. He fled to Peru in 2017 to seek political asylum.

There, he met with a friend from his undergraduate days at the Central University of Ecuador. He had no money to fight the charges against him, and had been forced to leave behind his wife and two young children.

“He felt bullied and diminished,” said the friend, Grace Jaramillo, who is now a political scientist at the University of British Columbia.

But later that year, Mr. Correa left office, and Mr. Villavicencio returned home. He won a seat in the National Assembly, where he served until May, when the legislature was dissolved by President Guillermo Lasso, who was facing impeachment proceedings over embezzlement accusations.

Mr. Lasso’s move also triggered a presidential election, with a vote set for Aug. 20. For his presidential run, Mr. Villavicencio, 59, cast himself as the anticorruption candidate. He was representing the Build Ecuador Movement, a broad coalition, and also campaigned on issues like personal safety, in a country that has been consumed by violence related to narco-trafficking.

Mr. Villavicencio was polling near the middle of an eight-person race, but remained hopeful about his chances, according to Ms. Jaramillo. But he was gunned down before voters could deliver their verdict.

Soon after the killing, Mr. Correa, the former president, issued a lament on social media.

“They have assassinated Fernando Villavicencio,” Mr. Correa wrote. “Ecuador has become a failed state.”