Most of the answers on this thread are either complex or will result in deadlock.
Following method is simple and it will avoid deadlock because we are waiting for the task to finish and only then getting its result-
var task = Task.Run(() => GenerateCodeAsync());
task.Wait();
string code = task.Result;
Furthermore, here is a reference to MSDN article that talks about exactly same thing- https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/