提示コードの/////コメント部内部のコードですがawait のコードで処理を待機せずそのまま無限ループを抜けてMain()関数の処理を終えてしまう原因が知りたいです。下記の参考サイトでawaitの待ち方について調べましたがどうも間違っていません、またtry catchしていますが例外とは出ていません。これはなぜでしょうか?おそらく非同期処理関係だと思うですが原因がわかりません
リファレンス: https://learn.microsoft.com/ja-jp/dotnet/api/system.net.http.httpclient.getstreamasync?view=net-7.0#system-net-http-httpclient-getstreamasync(system-uri-system-threading-cancellationtoken)
WebAPI: https://docs.joinmastodon.org/methods/timelines/streaming/#websocket-a-idwebsocketa
Task 待ち方: https://qiita.com/takutoy/items/d45aa736ced25a8158b3
cs
1using System.Diagnostics; 2using System.Net.Http.Headers; 3using System.Net.Http.Json; 4using System.Net; 5using System.Text.Json.Serialization; 6using System; 7using Newtonsoft.Json; 8using System.Net.Http; 9using Microsoft.AspNetCore.Http; 10using System.Security.Cryptography; 11using static Mastodon_test.Program; 12 13namespace Mastodon_test 14{ 15 public class Program 16 { 17 private static Client client; 18 19 20 public static async void f() 21 { 22 HttpClient client = new HttpClient(); 23 client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("test/1.0")); 24 25 //client.Timeout = TimeSpan.FromSeconds(5); 26 string stockSymbol = "GET"; 27 string url = $"https://mstdn.jp/api/v1/streaming/?stream=public"; 28 29 30 31 32 while (true) 33 { 34 try 35 { 36/////////////////////////////////////////////////////////////////// 37 Console.WriteLine("Establishing connection"); 38 var t = await client.GetStreamAsync(url); 39 //var t = client.GetStreamAsync(url).Result; 40 Console.WriteLine("Establishing connection"); 41/////////////////////////////////////////////////////////////////// 42 var streamReader = new StreamReader(t); 43 44 45 46 47 while (true) 48 //while (!streamReader.EndOfStream) 49 { 50 var message = await streamReader.ReadLineAsync(); 51 Console.WriteLine($"Received price update: {message}"); 52 } 53 54 } 55 catch (Exception ex) 56 { 57 //Here you can check for 58 //specific types of errors before continuing 59 //Since this is a simple example, i'm always going to retry 60 Console.WriteLine($"Error: {ex.Message}"); 61 Console.WriteLine("Retrying in 5 seconds"); 62 // await Task.Delay(TimeSpan.FromSeconds(5)); 63 } 64 } 65 66 67 } 68 69 70 public static async void t() 71 { 72 } 73 74 75 static void Main(string[] args) 76 { 77 f(); 78 79 80 } 81 82 83 } 84 85
こちらの質問が複数のユーザーから「調査したこと・試したことが記載されていない質問」という指摘を受けました。