A gay British soldier says Prince Harry rescued him from a homophobic attack by infantry members during a training exercise in Canada.

Trooper James Wharton became the target of abuse in 2008 after bragging about "scoring" with a fellow soldier.

To the defence: Prince Harry during his recent tour of duty to Afghanistan. Credit:Getty Images

"I think I'm about to be murdered by the infantry," Trooper Wharton told Prince Harry who was his tank commander at the time.

After hearing the story the third-in-line to the throne reportedly declared: "Right, I'm going to sort this shit out once and for all."