C# 在可移植类库中实现带回退的重试

C# 在可移植类库中实现带回退的重试,c#,.net,multithreading,portable-class-library,windows-8.1-universal,C#,.net,Multithreading,Portable Class Library,Windows 8.1 Universal,我正在开发一个可移植类库(PCL)。此PCL的目标是注册推送通知通道,并启动与服务器的MQTT连接。问题在于,PCL必须能够检测到瞬时连接故障,并通过退避重试。为此,我找到了一个名为Polly的库PCL有一个静态方法(Init),我的Windows Universal 8.1应用程序从OnLaunched方法调用它。我希望PCL不会阻止UI线程。我不能在OnLaunched方法中使用ConfigureAwait(false),因为只有UI线程可以访问与UI相关的元素。我认为当前的Pollyasy

我正在开发一个可移植类库(PCL)。此
PCL
的目标是注册推送通知通道,并启动与服务器的
MQTT
连接。问题在于,
PCL
必须能够检测到瞬时连接故障,并通过退避重试。为此,我找到了一个名为Polly的库
PCL
有一个静态方法(
Init
),我的
Windows Universal 8.1应用程序从
OnLaunched
方法调用它。我希望PCL不会阻止
UI线程
。我不能在
OnLaunched
方法中使用
ConfigureAwait(false)
,因为只有
UI线程
可以访问与
UI
相关的元素。我认为当前的
Polly
async实现阻塞了
UI线程

用这种假设实现我的PCL库的正确方法是什么?在
PCL
中是否有除
Polly
之外的用于重试Purose的库

App.xaml.xs PCL.cs
公共密封类通知程序
{
公共静态异步任务初始化(某些参数)
{
Policy Policy=Policy.Handle().WaitAndRetryAsync(5,
重试尝试=>TimeSpan.FromSeconds(Math.Pow(2,重试尝试)),(异常,TimeSpan)=>
{
Debug.WriteLine(异常);
Debug.WriteLine(timeSpan);
});
ExecuteAsync(()=>instance.OpenChannelAndUploadAsync(instance.ServerUrl));
wait instance.InitiateMqtt();
返回true;
}
    protected override async void OnLaunched(LaunchActivatedEventArgs e)
    {
        // some code

        await Notifier.Init(some parameters);

        // if I use ConfigureAwait(false) I see exceptions in rest of method
        // some code
    }
public sealed class Notifier
{

    public static async Task<bool> Init(some parameters)
    {

        Policy policy = Policy.Handle<NullReferenceException>().WaitAndRetryAsync(5,
                               retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception,timeSpan) =>
                               {
                                   Debug.WriteLine(exception);
                                   Debug.WriteLine(timeSpan);
                               });

        policy.ExecuteAsync(() => instance.OpenChannelAndUploadAsync(instance.ServerUrl));

        await instance.InitiateMqtt();


        return true;
    }