尽管当前 Mono
项目的 ServicePointManager
类在其接口(interface)中启用了 DnsRefreshTimeout
属性。相关属性未实现。
调用示例:
ServicePointManager.DnsRefreshTimeout = 10*60*1000; // 10 minutes
运行我的应用程序时,我在运行时遇到下一个异常:
The requested feature is not implemented. (System.NotImplementedException) at System.Net.ServicePointManager.set_DnsRefreshTimeout (Int32 value) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/ServicePointManager.cs:213
下面是实际的实现:
[MonoTODO]
public static int DnsRefreshTimeout
{
get {
throw GetMustImplement ();
}
set {
throw GetMustImplement ();
}
}
我认为我没有足够的知识来独自实现这个功能,因为我从上个月开始开发 C# Mono 应用程序。
那么,有人知道解决这个问题的方法吗?或者我应该为 Mono 项目团队请求功能实现吗?
我正在开发 Xamarin 跨平台应用程序,我确实需要缓存我的 DNS 解析至少 10 分钟。
P.s.请求的功能 https://bugzilla.xamarin.com/show_bug.cgi?id=11424
最佳答案
我没有解决办法,但我有一个解决方法:
var request = (HttpWebRequest)WebRequest.Create("http://www.google.com/");
// Disable KeepAlive so we don't reuse connections
request.KeepAlive = false;
// Clear out the cached host entry
var hostField = typeof(ServicePoint).GetField("host", BindingFlags.NonPublic | BindingFlags.Instance);
var hostFieldLock = typeof(ServicePoint).GetField("hostE", BindingFlags.NonPublic | BindingFlags.Instance);
var hostLock = hostFieldLock.GetValue(request.ServicePoint);
lock (hostLock)
hostField.SetValue(request.ServicePoint, null);
这是基于当前版本的 Mono 的 ServicePoint,您可以查看 here截至 2015 年 3 月。
关于c# - Mono 的 DNS 刷新超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15643223/