Mandrill版本3 Ping()方法 [英] Mandrill version 3 Ping() method

查看:82
本文介绍了Mandrill版本3 Ping()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前的Mandrill版本中,我使用了以下代码(基于ASP.NET框架的C#编写):

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;

将Mandrill版本更新为3后,如何从Ping()方法获取"PONG"消息,以前的Ping()方法具有返回类型字符串,但是现在在Mandrill版本3中,Ping()方法具有返回类型为Task<string>.我尝试调试代码,但找不到消息"PONG".

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".

我尝试用Google搜索,但发现的可能解决方案不起作用.

I tried to google and the possible solutions I found did not work.

我也尝试过string pingResult = mapi.Ping().Result;,它返回一个字符串,但是会引发异常.

Also I tried string pingResult = mapi.Ping().Result; which returns a string but it throws an exception.

任何人都可以帮忙吗?

编辑:我在以下方面面临僵局:

I am facing the deadlock in the following:

没有ConfigureAwait(false)会出现相同的问题.如上面的屏幕快照所述,此时页面将无限加载,并且不会显示任何结果.

Without ConfigureAwait(false) getting the same issue. At this point as mentioned in above screenshot, the page loads infinitely and no result shows up.

推荐答案

如果任何方法的返回类型为Task<T>Task,则可以等待该方法.进行此更改可能是为了使Ping花费一段时间(例如,超时)不会长时间阻塞线程.

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.

要获取字符串,可以await方法调用:

To get the string, you can either await the method call:

string pingResult = await mapi.Ping();

但是您只能在async上下文中执行此操作,因此如果Method声明标记为异步.或者,如果您的上下文是同步的,则可以执行以下操作:

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();

这篇关于Mandrill版本3 Ping()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆