18
18
using Microsoft . OpenApi . OData ;
19
19
using System . Net ;
20
20
using System . Xml ;
21
+ using System . Threading . Tasks ;
21
22
22
23
namespace OoasGui
23
24
{
@@ -50,28 +51,28 @@ public MainForm()
50
51
oasRichTextBox . WordWrap = false ;
51
52
}
52
53
53
- private void jsonRadioBtn_CheckedChanged ( object sender , EventArgs e )
54
+ private async void jsonRadioBtn_CheckedChanged ( object sender , EventArgs e )
54
55
{
55
56
Format = OpenApiFormat . Json ;
56
- Convert ( ) ;
57
+ await Convert ( ) ;
57
58
}
58
59
59
- private void yamlRadioBtn_CheckedChanged ( object sender , EventArgs e )
60
+ private async void yamlRadioBtn_CheckedChanged ( object sender , EventArgs e )
60
61
{
61
62
Format = OpenApiFormat . Yaml ;
62
- Convert ( ) ;
63
+ await Convert ( ) ;
63
64
}
64
65
65
- private void v2RadioBtn_CheckedChanged ( object sender , EventArgs e )
66
+ private async void v2RadioBtn_CheckedChanged ( object sender , EventArgs e )
66
67
{
67
68
Settings . OpenApiSpecVersion = Version = OpenApiSpecVersion . OpenApi2_0 ;
68
- Convert ( ) ;
69
+ await Convert ( ) ;
69
70
}
70
71
71
- private void v3RadioBtn_CheckedChanged ( object sender , EventArgs e )
72
+ private async void v3RadioBtn_CheckedChanged ( object sender , EventArgs e )
72
73
{
73
74
Settings . OpenApiSpecVersion = Version = OpenApiSpecVersion . OpenApi3_0 ;
74
- Convert ( ) ;
75
+ await Convert ( ) ;
75
76
}
76
77
77
78
private void fromFileRadioBtn_CheckedChanged ( object sender , EventArgs e )
@@ -91,7 +92,7 @@ private void fromUrlRadioBtn_CheckedChanged(object sender, EventArgs e)
91
92
loadBtn . Enabled = true ;
92
93
}
93
94
94
- private void btnBrowse_Click ( object sender , EventArgs e )
95
+ private async void btnBrowse_Click ( object sender , EventArgs e )
95
96
{
96
97
OpenFileDialog openFileDialog = new OpenFileDialog ( ) ;
97
98
openFileDialog . Filter = "CSDL files (*.xml)|*.xml|All files (*.*)|*.*" ;
@@ -106,7 +107,7 @@ private void btnBrowse_Click(object sender, EventArgs e)
106
107
fileTextBox . Text = openFileDialog . FileName ;
107
108
csdlRichTextBox . Text = text ;
108
109
Settings . ServiceRoot = new Uri ( openFileDialog . FileName ) ;
109
- Convert ( ) ;
110
+ await Convert ( ) ;
110
111
}
111
112
catch ( Exception ex )
112
113
{
@@ -119,7 +120,7 @@ private void btnBrowse_Click(object sender, EventArgs e)
119
120
}
120
121
}
121
122
122
- private void loadBtn_Click ( object sender , EventArgs e )
123
+ private async void loadBtn_Click ( object sender , EventArgs e )
123
124
{
124
125
string url = urlTextBox . Text ;
125
126
@@ -147,7 +148,7 @@ private void loadBtn_Click(object sender, EventArgs e)
147
148
LoadEdm ( url , csdl ) ;
148
149
csdlRichTextBox . Text = FormatXml ( csdl ) ;
149
150
Settings . ServiceRoot = requestUri ;
150
- Convert ( ) ;
151
+ await Convert ( ) ;
151
152
}
152
153
catch ( Exception ex )
153
154
{
@@ -163,6 +164,7 @@ private void LoadEdm(string resource, string text)
163
164
{
164
165
IEdmModel model ;
165
166
IEnumerable < EdmError > errors ;
167
+
166
168
if ( ! CsdlReader . TryParse ( XElement . Parse ( text ) . CreateReader ( ) , out model , out errors ) )
167
169
{
168
170
StringBuilder sb = new StringBuilder ( ) ;
@@ -178,19 +180,24 @@ private void LoadEdm(string resource, string text)
178
180
EdmModel = model ;
179
181
}
180
182
181
- private void Convert ( )
183
+ private async Task Convert ( )
182
184
{
183
185
if ( EdmModel == null )
184
186
{
185
187
return ;
186
188
}
187
189
188
- OpenApiDocument document = EdmModel . ConvertToOpenApi ( Settings ) ;
189
- MemoryStream stream = new MemoryStream ( ) ;
190
- document . Serialize ( stream , Version , Format ) ;
191
- stream . Flush ( ) ;
192
- stream . Position = 0 ;
193
- string openApi = new StreamReader ( stream ) . ReadToEnd ( ) ;
190
+ string openApi = null ;
191
+ await Task . Run ( ( ) =>
192
+ {
193
+ OpenApiDocument document = EdmModel . ConvertToOpenApi ( Settings ) ;
194
+ MemoryStream stream = new MemoryStream ( ) ;
195
+ document . Serialize ( stream , Version , Format ) ;
196
+ stream . Flush ( ) ;
197
+ stream . Position = 0 ;
198
+ openApi = new StreamReader ( stream ) . ReadToEnd ( ) ;
199
+ } ) ;
200
+
194
201
oasRichTextBox . Text = openApi ;
195
202
}
196
203
@@ -211,7 +218,7 @@ private string FormatXml(string xml)
211
218
return sw . ToString ( ) ;
212
219
}
213
220
214
- private void saveBtn_Click ( object sender , EventArgs e )
221
+ private async void saveBtn_Click ( object sender , EventArgs e )
215
222
{
216
223
SaveFileDialog saveFileDialog = new SaveFileDialog ( ) ;
217
224
if ( Format == OpenApiFormat . Json )
@@ -220,7 +227,7 @@ private void saveBtn_Click(object sender, EventArgs e)
220
227
}
221
228
else
222
229
{
223
- saveFileDialog . Filter = "YAML files (*.ymal )|*.json |All files (*.*)|*.*" ;
230
+ saveFileDialog . Filter = "YAML files (*.yaml )|*.yaml |All files (*.*)|*.*" ;
224
231
}
225
232
226
233
saveFileDialog . FilterIndex = 2 ;
@@ -231,31 +238,34 @@ private void saveBtn_Click(object sender, EventArgs e)
231
238
string output = saveFileDialog . FileName ;
232
239
using ( FileStream fs = File . Create ( output ) )
233
240
{
234
- OpenApiDocument document = EdmModel . ConvertToOpenApi ( Settings ) ;
235
- document . Serialize ( fs , Version , Format ) ;
236
- fs . Flush ( ) ;
241
+ await Task . Run ( ( ) =>
242
+ {
243
+ OpenApiDocument document = EdmModel . ConvertToOpenApi ( Settings ) ;
244
+ document . Serialize ( fs , Version , Format ) ;
245
+ fs . Flush ( ) ;
246
+ } ) ;
237
247
}
238
248
}
239
249
240
250
MessageBox . Show ( "Saved successful!" ) ;
241
251
}
242
252
243
- private void operationIdcheckBox_CheckedChanged ( object sender , EventArgs e )
253
+ private async void operationIdcheckBox_CheckedChanged ( object sender , EventArgs e )
244
254
{
245
255
Settings . EnableOperationId = ! Settings . EnableOperationId ;
246
- Convert ( ) ;
256
+ await Convert ( ) ;
247
257
}
248
258
249
- private void VerifyEdmModelcheckBox_CheckedChanged ( object sender , EventArgs e )
259
+ private async void VerifyEdmModelcheckBox_CheckedChanged ( object sender , EventArgs e )
250
260
{
251
261
Settings . VerifyEdmModel = ! Settings . VerifyEdmModel ;
252
- Convert ( ) ;
262
+ await Convert ( ) ;
253
263
}
254
264
255
- private void NavPathcheckBox_CheckedChanged ( object sender , EventArgs e )
265
+ private async void NavPathcheckBox_CheckedChanged ( object sender , EventArgs e )
256
266
{
257
267
Settings . EnableNavigationPropertyPath = ! Settings . EnableNavigationPropertyPath ;
258
- Convert ( ) ;
268
+ await Convert ( ) ;
259
269
}
260
270
}
261
271
}
0 commit comments