DNS Refresh Timeout with Mono

好久不见. 提交于 2019-12-22 17:40:41

问题


Although the current Mono project's ServicePointManager class has the DnsRefreshTimeout property enabled into its interface. The related property isn't implemented.

Sample call:

ServicePointManager.DnsRefreshTimeout = 10*60*1000; // 10 minutes

When running my application I get the next exception on runtime:

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

Here comes the actual implementation:

[MonoTODO]
public static int DnsRefreshTimeout
{
    get {
        throw GetMustImplement ();
    }
    set {
        throw GetMustImplement ();
    }
}

I think I don't have enough knowledge to implement this feature all by myself just because I am developing C# Mono applications since last month.

So, do anyone know one workaround for this? Or shall I request a feature implementation for the Mono project team?

I am developing a Xamarin cross-platform application and I really need to cache my DNS resolution at least for 10 minutes.

P.s. Feature requested at https://bugzilla.xamarin.com/show_bug.cgi?id=11424


回答1:


I don't have a fix, but I have a workaround:

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

This is based on the current version of ServicePoint for mono which you can view here as of March 2015.



来源:https://stackoverflow.com/questions/15643223/dns-refresh-timeout-with-mono

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!