I've not tried this but is seems to make sense"
from:
https://extension.oregonstate.edu/news/big-maggots-your-compost-theyre-soldier-fly-larvae
Soldier fly females lay eggs on the surface of nitrogen-rich material that is exposed. So, if you want to avoid having these large flies and their maggots in your compost pile, make sure you have enough leaves, dry grass, shredded paper and other organic "brown" material in the pile to cover the nitrogen food sources by at least two to four inches. Be sure to bury food scraps deeply in the pile and cover them well.
You can further discourage these flies by putting window screen over any holes in the bin and gluing it down with a waterproof caulking (like an exterior household caulk) on the inside of the bin to help exclude the flies in their egg laying stage.
They often thrive in worm bins, as well as compost bins, where they may out-compete the worms for food.
"In a worm bin, bury food scraps down at least six inches for the worms and let the flies eat what is on the surface," said Wise. "The flies don't eat the worms or their eggs so they aren't predators of the worms."
If that doesn't work, you might try a decoy bin for the flies that's stinkier and easier to get in. Or maybe start a worm bin indoors until it's pretty full of worms, then move the worms outside. Hopefully if the bin is mostly full of worms, there would less room for flies.
For us, the flies disappear in the winter. We happened to start our worm bins in the winter and they were full of worms by summer when the flies showed up. We haven't done anything to discourage flies and they seem to get along fine in our bins.