我是否应该在函数失败后重新调用 API 函数并在 c# 中刷新我的访问令牌?

Should I re call an API function after the function fails and I refresh my access token in c#?

抱歉,如果标题有点混乱,我有一个函数在我的应用程序启动时连接到 API:

private async void connectFunction()
{   
    try 
    {
        result = await myAPICall();
        // do a lot of stuff with the data here
    
    }
    catch (System.ArgumentNullException) // this is only throne when my api access token has expired
    {
        RefreshTokens();
        connectFunction(); //this is what I'm concerned about
    }



}

现在一切正常,但我不知道下一步该怎么做,我的访问令牌已更新,现在我需要再次调用相同的函数,但我担心如果我在catch 块可能会出错,我的应用程序会陷入循环,有什么建议吗?

如果您只想重试一次,则不需要循环(或递归):

private async void connectFunction()
{   
    MyResultType result;

    try 
    {
        result = await myAPICall();
    }
    catch (System.ArgumentNullException) // this is only throne when my api access token has expired
    {
        RefreshTokens();

        // If this fails, the exception is not caught here, which
        // is a good thing. We know that the token can't be the cause
        // because we just refreshed it, so we *want* this error to
        // bubble up to our generic UI exception handler.
        result = await myAPICall(); 
    }

    // do a lot of stuff with the data here
}