1010using  System . Threading . Tasks ; 
1111using  Microsoft . OpenApi . Interfaces ; 
1212using  Microsoft . OpenApi . Models ; 
13+ using  Microsoft . VisualStudio . Threading ; 
1314
1415namespace  Microsoft . OpenApi . Reader 
1516{ 
@@ -19,6 +20,8 @@ namespace Microsoft.OpenApi.Reader
1920    public  static class  OpenApiModelFactory 
2021    { 
2122        private  static readonly  HttpClient  _httpClient  =  new ( ) ; 
23+         private  static readonly  JoinableTaskContext  _joinableTaskContext  =  new ( ) ; 
24+         private  static readonly  JoinableTaskFactory  _joinableTaskFactory  =  new ( _joinableTaskContext ) ; 
2225
2326        static OpenApiModelFactory ( ) 
2427        { 
@@ -33,7 +36,7 @@ static OpenApiModelFactory()
3336        /// <returns>An OpenAPI document instance.</returns> 
3437        public  static ReadResult  Load ( string  url ,  OpenApiReaderSettings  settings  =  null ) 
3538        { 
36-             return  LoadAsync ( url ,  settings ) . GetAwaiter ( ) . GetResult ( ) ; 
39+             return  _joinableTaskFactory . Run ( async   ( )   =>   await   LoadAsync ( url ,  settings ) ) ; 
3740        } 
3841
3942        /// <summary> 
@@ -49,7 +52,9 @@ public static ReadResult Load(Stream stream,
4952        { 
5053            settings  ??=  new  OpenApiReaderSettings ( ) ; 
5154
52-             var  result  =  LoadAsync ( stream ,  format ,  settings ) . GetAwaiter ( ) . GetResult ( ) ; 
55+             // Run the async method synchronously using JoinableTaskFactory 
56+             var  result  =  _joinableTaskFactory . Run ( async  ( )  =>  await  LoadAsync ( stream ,  format ,  settings ) ) ; 
57+             
5358            if  ( ! settings . LeaveStreamOpen ) 
5459            { 
5560                stream . Dispose ( ) ; 
@@ -69,7 +74,9 @@ public static ReadResult Load(TextReader input,
6974                                      string  format , 
7075                                      OpenApiReaderSettings  settings  =  null ) 
7176        { 
72-             return  LoadAsync ( input ,  format ,  settings ) . GetAwaiter ( ) . GetResult ( ) ; 
77+             // Run the async method synchronously using JoinableTaskFactory 
78+             var  result  =  _joinableTaskFactory . Run ( async  ( )  =>  await  LoadAsync ( input ,  format ,  settings ) ) ; 
79+             return  result ; 
7380        } 
7481
7582        /// <summary> 
@@ -81,7 +88,7 @@ public static ReadResult Load(TextReader input,
8188        public  static async  Task < ReadResult >  LoadAsync ( string  url ,  OpenApiReaderSettings  settings  =  null ) 
8289        { 
8390            var  format  =  GetFormat ( url ) ; 
84-             var  stream  =  await  GetStream ( url ) ; 
91+             var  stream  =  await  GetStreamAsync ( url ) ; 
8592            return  await  LoadAsync ( stream ,  format ,  settings ) ; 
8693        } 
8794
@@ -145,7 +152,24 @@ public static ReadResult Parse(string input,
145152            format  ??=  OpenApiConstants . Json ; 
146153            settings  ??=  new  OpenApiReaderSettings ( ) ; 
147154            using  var  reader  =  new  StringReader ( input ) ; 
148-             return  LoadAsync ( reader ,  format ,  settings ) . GetAwaiter ( ) . GetResult ( ) ; 
155+ 
156+             return  _joinableTaskFactory . Run ( async  ( )  =>  await  ParseAsync ( input ,  reader ,  format ,  settings ) ) ; 
157+         } 
158+ 
159+         /// <summary> 
160+         /// An Async method to prevent synchornously blocking the calling thread. 
161+         /// </summary> 
162+         /// <param name="input"></param> 
163+         /// <param name="reader"></param> 
164+         /// <param name="format"></param> 
165+         /// <param name="settings"></param> 
166+         /// <returns></returns> 
167+         public  static async  Task < ReadResult >  ParseAsync ( string  input , 
168+                                        StringReader  reader , 
169+                                        string  format  =  null , 
170+                                        OpenApiReaderSettings  settings  =  null ) 
171+         { 
172+             return  await  LoadAsync ( reader ,  format ,  settings ) ; 
149173        } 
150174
151175        /// <summary> 
@@ -183,7 +207,9 @@ public static T Load<T>(string url, OpenApiSpecVersion version, out OpenApiDiagn
183207        { 
184208            var  format  =  GetFormat ( url ) ; 
185209            settings  ??=  new  OpenApiReaderSettings ( ) ; 
186-             var  stream  =  GetStream ( url ) . GetAwaiter ( ) . GetResult ( ) ; 
210+ 
211+             var  stream  =  _joinableTaskFactory . Run ( async  ( )  =>  await  GetStreamAsync ( url ) ) ; 
212+ 
187213            return  Load < T > ( stream ,  version ,  format ,  out  diagnostic ,  settings ) ; 
188214        } 
189215
@@ -227,7 +253,8 @@ private static string GetContentType(string url)
227253        { 
228254            if  ( ! string . IsNullOrEmpty ( url ) ) 
229255            { 
230-                 var  response  =  _httpClient . GetAsync ( url ) . GetAwaiter ( ) . GetResult ( ) ; 
256+                 var  response  =  _joinableTaskFactory . Run ( async  ( )  =>  await  _httpClient . GetAsync ( url ) ) ; 
257+                 //var response = _httpClient.GetAsync(url).GetAwaiter().GetResult(); 
231258                var  mediaType  =  response . Content . Headers . ContentType . MediaType ; 
232259                return  mediaType . Split ( ";" . ToCharArray ( ) ,  StringSplitOptions . RemoveEmptyEntries ) . First ( ) ; 
233260            } 
@@ -260,7 +287,7 @@ public static string GetFormat(string url)
260287            return  null ; 
261288        } 
262289
263-         private  static async  Task < Stream >  GetStream ( string  url ) 
290+         private  static async  Task < Stream >  GetStreamAsync ( string  url ) 
264291        { 
265292            Stream  stream ; 
266293            if  ( url . StartsWith ( "http" ,  StringComparison . OrdinalIgnoreCase )  ||  url . StartsWith ( "https" ,  StringComparison . OrdinalIgnoreCase ) ) 
@@ -297,6 +324,5 @@ SecurityException or
297324
298325            return  stream ; 
299326        } 
300- 
301327    } 
302328} 
0 commit comments