简体   繁体   English

自动重试System.Net.Http.HttpClient

[英]Automatic retry for the System.Net.Http.HttpClient

I know Entity Framework has a 'Connection Resiliency and Retry Logic' mechanism to work with unreliable networks. 我知道实体框架具有“连接弹性和重试逻辑”机制,可用于不可靠的网络。

Is there also such a thing for the System.Net.Http.HttpClient? System.Net.Http.HttpClient也有这样的事情吗? So a sort of an automatic retry mechanism so when the connections gets lost or a timeout occurs, then it will retry for x times. 因此,这是一种自动重试机制,因此当连接丢失或发生超时时,它将重试x次。

Is that possible? 那可能吗?

As far as I know, there is no such mechanism. 据我了解,还没有这种机制。 But you can write yourself an event, checking the connectivity all x milliseconds. 但是您可以编写一个事件,并在所有x毫秒内检查连接性。 like 喜欢

public class CheckInternet
{
    public CheckInternet()
    {
        Timer t1 = new Timer();
        t1.Interval = 1000;
        t1.Tick += T1_Tick;
        t1.Start();
    }

    private void T1_Tick(object sender, EventArgs e)
    {
        var ping = new System.Net.NetworkInformation.Ping();
        var result = ping.Send("www.google.com");
        if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
        {
            // Your code to reconnect here
        }
    }
    public event EventHandler CheckConnectivity;
    private void OnCheckConnectivity()
    {
        CheckConnectivity?.Invoke(this, new EventArgs());
    }
}

Now just implement this to your other class use 现在,将其实现为其他类使用

CheckInternet cki = new CheckInternet();
cki.CheckConnectivity += new cki_CheckConnectivity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM