.NET Core / .NET Standard 2.0中的ReliableSession / MaxRetryCount

时间:2018-03-15 16:33:09

标签: c# wcf .net-core wcf-binding .net-standard

我已将Connected Service(WCF客户端)添加到.NET Standard 2.0项目中。

我正在尝试设置wcf绑定,以便在存在Communication Exception时,它将重试该请求。像https://stackoverflow.com/a/1968874/475727

这样的东西

如何在.NET Standard 2.0中执行此操作?

1 个答案:

答案 0 :(得分:0)

.NET Core尚不支持可靠的会话。从某种意义上说,你基本上是在自己的基础上,你需要实现这背后的业务逻辑。也许这有助于你:

public static void Retry(int retryCount, TimeSpan delay, Action action)
    {
        bool b = Retry<bool>(
            retryCount,
            delay,
            () => { action(); return true; });
    }

    public static T Retry<T>(int retryCount, TimeSpan delay, Func<T> func)
    {
        int currentAttempt = 0;

        while (true)
        {
            try
            {
                ++currentAttempt;
                return func();
            }
            catch
            {
                if (currentAttempt == retryCount)
                {
                    throw;
                }
            }

            if (delay > TimeSpan.Zero)
            {
                Thread.Sleep(delay);
            }
        }
    }