When using IObservable.LastAsync()
to force my console app to wait on the result of an API call using Flurl, that API call is never made and the main thread deadlocks and never returns from LastAsync()
. My goals are:
- Since this is a console app, I can't really "subscribe" to the API call since that would allow the main thread to continue, likely causing it to exit prior to the API call completing. So I need to block until the value is obtained.
- The API call should be deferred until the first subscriber requests a value.
- Second and onward subscribers should not cause another API call, instead the last value from the stream should be returned (this is the goal of using
Replay(1)
)
Here is an example that reproduces the issue:
public static class Program
{
public static async Task Main(string[] args)
{
var obs = Observable.Defer(() =>
"https://api.publicapis.org"
.AppendPathSegment("entries")
.GetJsonAsync()
.ToObservable())
.Select(x => x.title)
.Replay(1);
var title = await obs.LastAsync();
Console.WriteLine($"Title 1: {title}");
}
}
How can I modify my example to ensure that all 3 requirements above are met? Why does my example cause a deadlock?