The approach you describe sounds reasonable. Without knowing exactly what the error is when "it returns failure right away" it's impossible to say why it isn't working as expected. Can you give us the error log when it does fail?
Having an actual copy of your code would help a little bit. It would help even more if it would be possible to get a copy of your WPF application, allow us to run it on our machines then run your test and diagnose why it's not working as expected. Then we can come up with a solution for you.