Are you running your code in Visual Studio?
In this case, the port / url wont be localhost:5001.
You need to provide the hostname and port as follows -
string hostName = Application.Current.Host.Source.Host;
if (Application.Current.Host.Source.Port != 80)
hostName += ":" + Application.Current.Host.Source.Port;