Polly icon indicating copy to clipboard operation
Polly copied to clipboard

Make *Async methods available for .NET 4.0 targeted projects.

Open Lumirris opened this issue 10 years ago • 75 comments

Given that .NET 4.0 targeted projects can utilize Tasks and the await/async keywords through Microsoft.Bcl.Async, it would be nice to make the *Async methods available for .NET 4.0 projects that use the Polly Nuget package.

Lumirris avatar Apr 15 '16 18:04 Lumirris

@Lumirris Please feel free to take yourself a fork and try this out. The availability / inclusion of the async overloads is controlled by a compilation constant SUPPORTS_ASYNC. In the .NET4.0 proj, include Microsoft.Bcl.Async, add SUPPORTS_ASYNC as a conditional compilation constant, and you should be good to go.

reisenberger avatar Apr 18 '16 20:04 reisenberger

Hi @Lumirris Having consulted internally at App-vNext, current view is not to take extra dependencies in the core Polly project, as we're targeting .NET Core compatibility and reducing the overall number of separate projects / dependencies. Will however leave the issue open to see if there is greater demand.

That said, it should be fairly easy to maintain your own fork for this and rebase it when you want to pick up new features.

Happy for any feedback on how Polly async plays with Microsoft.Bcl.Async (shouldn't be any problems), from anything you try ...

reisenberger avatar Apr 19 '16 20:04 reisenberger

Hi @reisenberger I wonder if a simple solution would be to create another Nuget package like Polly.Net40Async that would leverage existing *Async code and use another project file like src/Polly.Net45/Polly.Net40Async.csproj which included

<DefineConstants>TRACE;DEBUG;SUPPORTS_ASYNC;SUPPORTS_READONLY_COLLECTION</DefineConstants>

I haven't done something like this before, but if you think the concept is sound, I'll work on it. Does the idea of a separate Nuget package sounds good?

Lumirris avatar Apr 20 '16 13:04 Lumirris

Hi @Lumirris To make code suitable for a .NET4.0Async nuget package, I would follow the principles:

  • only include a .NET4.0 proj (not 4.5, 3.5, PCL), so there isn't any confusion that this package provides anything other than specifically the .NET4.0Async version
  • only include one .NET4.0 proj in the package (wasn't sure if you were suggesting src/Polly.Net45/Polly.Net40Async.csproj (45 a typo?) as additional to the existing .NET40 package or replacement; suggest replacement).

Try the following steps:

  • take your own fork of Polly
  • remove all projects except for the .NET40 project, Polly.Shared and Polly.SharedSpecs
  • rename the .NET40 project in the solution as Polly.Net40Async.csproj as you suggest (makes it v clear)
  • reference Microsoft.Bcl.Async from that csproj
  • add SUPPORTS_ASYNC as a conditional compilation constant to that proj

Let us know how you get on.

reisenberger avatar Apr 25 '16 08:04 reisenberger

Hey @Lumirris We talked further at AppvNext, I believe from @joelhulen we'd be happy to host and maintain (ie keep udpated) a Nuget package like Polly.Net40Async, as you suggest - assuming we can get all components to play.

@Lumirris Did you have any luck trying out a prototype, per previous notes? (if you have an immediate need for this, you'd be well placed to test :smile: ). Do you need any further assistance?

@joelhulen I see this clearest as a separate package, not part of the main Polly package, to reduce the dependencies of the main Polly package.

Thanks!

reisenberger avatar Apr 27 '16 18:04 reisenberger

@reisenberger @joelhulen I'll get working on this tonight and share a link to my fork before doing a pull request to fix up anything you spot that needs work.

Lumirris avatar Apr 28 '16 19:04 Lumirris

@reisenberger @joelhulen I forgot about the fact that Microsoft.Bcl.Async gives you some of the async features through TaskEx vs. Task. The consequences of this are that the following lines of code in Polly.Shared would need to be changed just for this Nuget package, which would put it at odds with the other projects using these files:

-SystemClock.cs (here and here) -RetryPolicyStateAsync.cs -RetryPolicyStateWithCountAsync.cs

My thought is to replace calls to these async methods with calls to wrapping methods placed in a centralized location where the compiler constant switches are done, and utilize a new compiler constant, SUPPORTS_ASYNC_40 to distinguish between .NET 4.5 async support and .NET 4.0 async support through the Microsoft.Bcl.Async package and TaskEx.

I'll post a link to my fork before making a PR for your review - please yank on the emergency brake if this seems like a bad path to take.

Lumirris avatar May 02 '16 15:05 Lumirris

Hey @Lumirris. Thanks for all your work on this!

With you about Task vs TaskEx in Microsoft.Bcl.Async. SUPPORTS_ASYNC_40 switch sounds good.

Am assuming (shout if I misunderstood) that SUPPORTS_ASYNC_40 should supplement (rather than replace) SUPPORTS_ASYNC as a compilation constant in the new package, so the Polly.Net40Async.csproj would set both SUPPORTS_ASYNC and SUPPORTS_ASYNC_40. This will certainly reduce modifications elsewhere.

Like the idea of centralising the Task vs TaskEx switches - just a slight tweak to suggest. Rather than wrapping methods, can we follow the pattern that's already in SystemClock.cs? IE Define a public static Action<...> and vary the definition of that. So we might have a new class TaskHelpers in Utilities folder, the whole class definition wrapped in #if SUPPORTS_ASYNC / #endif, and then definitions like:

        /// <summary>
        /// Allows the setting of a custom async Sleep implementation for testing.
        /// By default this will be a call to <see cref="M:Task.Delay"/>
        /// </summary>
#if !SUPPORT_ASYNC_40
        public static Func<TimeSpan, CancellationToken, Task> SleepAsync = Task.Delay;
#else
        public static Func<TimeSpan, CancellationToken, Task> SleepAsync = TaskEx.Delay;
#endif

@Lumirris What do you think? Does this all sound good?

reisenberger avatar May 02 '16 19:05 reisenberger

I really do like the pattern @reisenberger proposes here, which matches the existing SystemClock solution. This will cause the least impact on other areas of the solution. Either way, I really appreciate your contribution!

joelhulen avatar May 02 '16 20:05 joelhulen

@Lumirris @joelhulen I put some investigation into the unit-testing for an async 4.0 package.

It turns out .NET4.0 unit tests were removed back in October 2015 because the latest XUnit 2.0+, which Polly uses, supports only 4.5+. In other words, for the validity of the .NET4.0 and 3.5 packages we currently offer, we assume backwards .NET compatibility: that the supported .NET3.5 / .NET4.0 behaviours behave the same as the .NET4.5 and PCL tests verify.

Following the same logic, for the new .NET40async package, we would be relying additionally on the Microsoft.Bcl.Async TaskEx functionality behaving as the later Task functionality does. Given the Microsoft.Bcl.Async package was the community technology preview for the relevant .Net4.5 elements anyway - and @Lumirris has identified the only switches will be around Task.FromResult and Task.Delay - I think this is acceptable. Just @joelhulen sharing.

Alternatives could be ( ... but I propose we don't adopt either; they are not particularly palatable from the p-o-view of long-term maintenance ...) :

  • Downgrade both the master Polly package and the .NET40 async package to the earlier XUnit 1.9.2, and remove the XUnit 2.0+ features we use. Not proposing taking / holding the overall package back like this. [or]
  • Maintain divergent sets of tests (on different versions of xunit) between the two different packages. Maintenance headache.

If anyone has a brighter idea, please shout. :smile:

EDIT: Xunit and .NET4.0 reference: https://github.com/xunit/xunit/issues/241

reisenberger avatar May 02 '16 20:05 reisenberger

@reisenberger I'm in agreement with all of your recent suggestions pertaining to dealing with Task vs. TaskEx. I had started to go down that road already, and even experimented with using the compiler constant switch around the new using static syntax with Task and TaskEx, since the methods are named the same. Somehow that seems like an abuse, though, and brittle because it only works when the methods are named the same:

#if SUPPORTS_ASYNC
using System.Threading.Tasks;
#if SUPPORTS_ASYNC_NET40
using static System.Threading.Tasks.TaskEx;
#else
using static System.Threading.Tasks.Task;
#endif
#endif

namespace Polly.Utilities
{
    /// <summary>
    ///     Time-related delegates used to abstract away the async implementations
    /// </summary>
    public static class SystemClock
    {
#if SUPPORTS_ASYNC
        /// <summary>
        ///     Allows the setting of a custom async Sleep implementation for testing.
        ///     By default this will be a call to <see cref="M:Task.Delay" /> 
        ///     or <see cref="M:TaskEx.Delay" /> depending on the targeted framework.
        /// </summary>
        public static Func<TimeSpan, CancellationToken, Task> SleepAsync = Delay;
#endif
   }
}

Lumirris avatar May 03 '16 14:05 Lumirris

@Lumirris Great. Agree with your sentiments about using static. The method names probably hold (because Microsoft.Bcl.Async was the community tech prev for the relevant .Net4.5 elements), but it isn't guaranteed or necessarily as clear. Always good to and happy to explore options, tho. :wink:

reisenberger avatar May 03 '16 17:05 reisenberger

@Lumirris Huge thanks for offering to take on this Microsoft.Bcl.Async work! How are you progressing with this? Any blocks? Do you need any assistance?

@KBRETON1 Are you (having expressed an interest under #90) available to assist with this? (if we need any assistance). EDIT: @KBRETON1: Have a read of the thread above to see what's involved!

@Lumirris Re my reference to this issue from #107: I have constructed the #108 fix for #107 so that users of Polly with .Net40 and Microsoft.Bcl.Async in VS2010 would be able to specify the new async onRetry delegates cleanly (by either of the two methods proposed in my comment https://github.com/App-vNext/Polly/issues/107#issuecomment-218895978), despite the VS2010 limitation. So, my comments in #107 are not an issue for progressing this.

Many thanks!

reisenberger avatar May 25 '16 21:05 reisenberger

@Lumirris is there a hosted branch that I could clone to get up to speed on this thread? I am new to this project and willing to help. I am in general agreement with the topics above.

KBRETON1 avatar May 26 '16 12:05 KBRETON1

@KBRETON1 I'm finishing up my work on this today; I'll push it up to my own fork, and have you and others review it before I do a pull request. Also, I see that my forked 'master' branch is now several commits behind the main 'master' branch. I'm new to open source contribution, and I'm not sure how I should proceed from here - I can't 'rebase' my forked master, since it too is public. Do I simply submit my pull request and @reisenberger would handle merge conflicts?

Lumirris avatar May 26 '16 13:05 Lumirris

@Lumirris Thanks for your contribution! As far as handling merge conflicts and rebasing, that responsibility usually falls on the contributor submitting a pull request. The steps are simple and outlined on our wiki page. If you get stuck, simply reply to this thread and we'll do our best to help you sort it out.

joelhulen avatar May 26 '16 14:05 joelhulen

@Lumirris Thanks for all your work on this, and @KBRETON1, thanks for your support/involvement! @KBRETON1 Please (as another user wanting the .NET40 support) do feel free to review alongside me, at review stage.

@Lumirris For now, as you suggest, carry on pushing your changes to your master, to complete the work. Will be at least clear to review there, before we engage in rebasing.

@joelhulen The final requirement will be to publish a separate Polly.Net40Async nuget package. That Polly.Net40Async nuget package will take the Microsoft.BCL.Async dependency, but, as we know, the main nuget Polly package mustn't. Can you from your nuget script/process publish both packages like that out of the one GitHub repo? Only asking as, if we have instead to split into two separate github repo sources (one without .NET40, one with), that may change the whole merge/rebase requirement anyway.

reisenberger avatar May 26 '16 15:05 reisenberger

@reisenberger Yes, we can. You'll see within the build.cake file where the __CreateNugetPackage and __CreateSignedNugetPackage tasks are defined. We would need to define a new NuGet package task for .Net40Async, ensuring the appropriate project/assemblies are referenced.

I agree that it would be best to try and keep everything within this repo. Worst case, we create a new branch, but hopefully we won't have to go that route.

joelhulen avatar May 26 '16 16:05 joelhulen

@joelhulen D'oh. Don't know why I didn't check the build.cake for that. Cool! Imagine yes we can (and should) keep it all in the same repo then.

reisenberger avatar May 26 '16 16:05 reisenberger

@reisenberger @joelhulen @KBRETON1 I've pushed the changes to my feature branch on my own fork. I'm not sure if you'll ultimately want the PR to come from my master branch or this feature branch, but I wanted to have the changes reviewed before the PR is made.

Comparing App-vNext Polly (master) to Lumirris Polly (async-dot-net-40)

Lumirris avatar May 26 '16 18:05 Lumirris