10
10
using System . Threading . Tasks ;
11
11
using Microsoft . OpenApi . Interfaces ;
12
12
using Microsoft . OpenApi . Models ;
13
- using Microsoft . VisualStudio . Threading ;
14
13
15
14
namespace Microsoft . OpenApi . Reader
16
15
{
@@ -20,8 +19,6 @@ namespace Microsoft.OpenApi.Reader
20
19
public static class OpenApiModelFactory
21
20
{
22
21
private static readonly HttpClient _httpClient = new ( ) ;
23
- private static readonly JoinableTaskContext _joinableTaskContext = new ( ) ;
24
- private static readonly JoinableTaskFactory _joinableTaskFactory = new ( _joinableTaskContext ) ;
25
22
26
23
static OpenApiModelFactory ( )
27
24
{
@@ -36,7 +33,9 @@ static OpenApiModelFactory()
36
33
/// <returns>An OpenAPI document instance.</returns>
37
34
public static ReadResult Load ( string url , OpenApiReaderSettings settings = null )
38
35
{
39
- return _joinableTaskFactory . Run ( async ( ) => await LoadAsync ( url , settings ) ) ;
36
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
37
+ return LoadAsync ( url , settings ) . GetAwaiter ( ) . GetResult ( ) ;
38
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
40
39
}
41
40
42
41
/// <summary>
@@ -52,9 +51,10 @@ public static ReadResult Load(Stream stream,
52
51
{
53
52
settings ??= new OpenApiReaderSettings ( ) ;
54
53
55
- // Run the async method synchronously using JoinableTaskFactory
56
- var result = _joinableTaskFactory . Run ( async ( ) => await LoadAsync ( stream , format , settings ) ) ;
57
-
54
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
55
+ var result = LoadAsync ( stream , format , settings ) . GetAwaiter ( ) . GetResult ( ) ;
56
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
57
+
58
58
if ( ! settings . LeaveStreamOpen )
59
59
{
60
60
stream . Dispose ( ) ;
@@ -74,8 +74,9 @@ public static ReadResult Load(TextReader input,
74
74
string format ,
75
75
OpenApiReaderSettings settings = null )
76
76
{
77
- // Run the async method synchronously using JoinableTaskFactory
78
- var result = _joinableTaskFactory . Run ( async ( ) => await LoadAsync ( input , format , settings ) ) ;
77
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
78
+ var result = LoadAsync ( input , format , settings ) . GetAwaiter ( ) . GetResult ( ) ;
79
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
79
80
return result ;
80
81
}
81
82
@@ -153,7 +154,9 @@ public static ReadResult Parse(string input,
153
154
settings ??= new OpenApiReaderSettings ( ) ;
154
155
using var reader = new StringReader ( input ) ;
155
156
156
- return _joinableTaskFactory . Run ( async ( ) => await ParseAsync ( input , reader , format , settings ) ) ;
157
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
158
+ return ParseAsync ( input , reader , format , settings ) . GetAwaiter ( ) . GetResult ( ) ;
159
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
157
160
}
158
161
159
162
/// <summary>
@@ -208,7 +211,9 @@ public static T Load<T>(string url, OpenApiSpecVersion version, out OpenApiDiagn
208
211
var format = GetFormat ( url ) ;
209
212
settings ??= new OpenApiReaderSettings ( ) ;
210
213
211
- var stream = _joinableTaskFactory . Run ( async ( ) => await GetStreamAsync ( url ) ) ;
214
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
215
+ var stream = GetStreamAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ;
216
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
212
217
213
218
return Load < T > ( stream , version , format , out diagnostic , settings ) ;
214
219
}
@@ -253,7 +258,10 @@ private static string GetContentType(string url)
253
258
{
254
259
if ( ! string . IsNullOrEmpty ( url ) )
255
260
{
256
- var response = _joinableTaskFactory . Run ( async ( ) => await _httpClient . GetAsync ( url ) ) ;
261
+ #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
262
+ var response = _httpClient . GetAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ;
263
+ #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
264
+
257
265
var mediaType = response . Content . Headers . ContentType . MediaType ;
258
266
return mediaType . Split ( ";" . ToCharArray ( ) , StringSplitOptions . RemoveEmptyEntries ) . First ( ) ;
259
267
}
0 commit comments