首页 > 解决方案 > 如何使用 C# 实现重试模式?

问题描述

我有一个代理服务器列表。使用这些代理服务器,我想点击 URL 以编程方式获取数据。我想在这里应用重试模式。例如,如果我无法从代理服务器 1 获取数据,我想使用代理服务器 2 访问 URL,依此类推。根据良好的设计实践,我应该在ProxyHelper 类 GetData 方法中写什么。

示例代码:

Interface IProxyServer {

    IResponse GetData(String URL);

}

public class ProxyServerA implements IProxyServer {

    public IResponse GetData(String URL)
    {
        //some code
    }

}

public class ProxyServerB implements IProxyServer {

    public IResponse GetData(String URL) {
        //some code
    }

}

public class ProxyHelper {

    public List<IProxyServer> _proxyservers = new List<IProxyServer>(); 
    
    public IResponse GetData(String URL)
    {
    
    // Following the best practices, what should be the code here which will implement retry mechanism
    // and call other available proxy server in case the request is failed.
    
    }
    
    public void Add(IProxyServer proxyserver)
    {
        _proxyservers.Add(proxyserver);
    }

}

void main() {

    ProxyHelper proxyHelper = new ProxyHelper();

    String URL = "www.google.com";
    
    proxyserver.Add(new ProxyServerA());
    
    proxyserver.Add(new ProxyServerB());

    var response = ProxyHelper.GetData(URL);

}

标签: c#design-patterns

解决方案


推荐阅读