问题
In the previous version of Mandrill, I was using the following piece of code ( written in C# based on ASP.NET framework):
MandrillApi mapi = new MandrillApi(
SettingsKeyInfoProvider.GetValue("MandrillAPIKey"),
SettingsKeyInfoProvider.GetBoolValue("MandrillUseSSL"),
SettingsKeyInfoProvider.GetIntValue("MandrillTimeout"));
// validate and respond
string pingResult = mapi.Ping();
if (pingResult.ToUpper().Contains("PONG!"))
return mapi;
After updating the Mandrill version to 3 and how can I get the "PONG" message from Ping() method, previously the Ping() method had a return type string, but now in Mandrill version 3 the Ping() method has the return type as Task<string>
. I tried debugging the code but not able to find the message as "PONG".
I tried to google and the possible solutions I found did not work.
Also I tried string pingResult = mapi.Ping().Result;
which returns a string but it throws an exception.
Can anyone please help?
Edit: I am facing the deadlock in the following:
Without ConfigureAwait(false)
getting the same issue. At this point as mentioned in above screenshot, the page loads infinitely and no result shows up.
回答1:
If the return type of any Method is Task<T>
or Task
you can await that method. This change was probably done so that if Ping takes a while (e.g if it times out) it wont be blocking the Thread for a long time.
To get the string, you can either await
the method call:
string pingResult = await mapi.Ping();
But you can only do this in an async
Context, so if the Method declaration is marked with async. Or, if your context is synchronous, you can do this:
string pingResult = mapi.Ping().GetAwaiter().GetResult();
来源:https://stackoverflow.com/questions/57524558/mandrill-version-3-ping-method