/
Default.aspx.cs
331 lines (283 loc) · 13.1 KB
/
Default.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AzureMLInterface.Model;
using AzureMLInterface.Controlers;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using System.Net;
using System.Net.Security;
using Newtonsoft.Json.Linq;
using ParameterIO;
namespace AzureMLRRSWebTemplate
{
public partial class Default : System.Web.UI.Page
{
AMLParameterObject paramObj = new AMLParameterObject();
string webServicePostUrl;
protected void Page_Load(object sender, EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if (paramObj.ImportInputParameter(Server.MapPath("~\\Resources\\AMLParameter.xml")))
{
Page.Title = paramObj.Title;
lblTitle.Text = paramObj.Title;
webServicePostUrl = AMLParameterObject.GetPostUrl(paramObj.Url);
}
else RequireInfor();
if (paramObj.listGlobalParameter != null && paramObj.listGlobalParameter.Count > 0)
// Show Global parameter
//GenerateControl.ShowInput(GlobalPlaceHolder1, GlobalPlaceHolder2, paramObj.listGlobalParameter, browser);
GenerateControl.ShowInput(PlaceHolderMain, paramObj.listGlobalParameter, browser, "Global Parameters");
//GenerateControl.ShowInput(InputPlaceHolder1, InputPlaceHolder2, paramObj.listInputParameter, browser);
if (paramObj.listInputGroup != null)
{
paramObj.listInputGroup.Sort(); // Sort input by group name
foreach (var groupName in paramObj.listInputGroup)
{
var listParam = paramObj.listInputParameter.Where(x => (x.Group == groupName)).ToList();
GenerateControl.ShowInput(PlaceHolderMain, listParam, browser, groupName + " Parameters");
}
}
}
private void RequireInfor()
{
Response.Redirect("Setting.aspx");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Dictionary<string, string> featureList = getColumnsAndValues();
//if (featureList == null || featureList.Count == 0) return;
InvokeRequestResponseService_A(featureList).Wait();
}
//Check and adjust URL version if necessary
private string setPostURLString()
{
//version 2 ends with exeucte. If passed in v2 URL, change to end with Score
int idx = webServicePostUrl.IndexOf("/execute");
if (idx > 0)
{
//remove everything after and including '/execute' and replace with '/score'
string temp = webServicePostUrl.Substring(0, (webServicePostUrl.Length - (webServicePostUrl.Length - idx)));
webServicePostUrl = temp + "/score";
}
return webServicePostUrl;
}
//Get the controls(columns) from the placeholder and get their values to submit to the API
private Dictionary<string, string> getColumnsAndValues()
{
var dict = new Dictionary<string, string>();
List<Control> listInput = new List<Control>();
foreach (Control control in InputPlaceHolder1.Controls)
listInput.Add(control);
foreach (Control control in InputPlaceHolder2.Controls)
listInput.Add(control);
foreach (Control control in listInput)
{
string columnName = "";
string columnValue = "";
if (control.ID != null)
{
columnName = control.ID;
if (control is TextBox || control is DropDownList || control is RadioButtonList)
{
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "")
columnValue = txt.Text;
}
else if (control is DropDownList)
{
DropDownList lb = control as DropDownList;
if (lb.SelectedIndex != -1)
columnValue = lb.SelectedValue;
}
else if(control is RadioButtonList)
{
RadioButtonList ct = control as RadioButtonList;
if (ct.SelectedIndex != -1)
columnValue = ct.SelectedValue;
}
//add to list
dict.Add(columnName, columnValue);
}
}
}
return dict;
}
private Dictionary<string, string> getGlobalParameters()
{
var globalParameters = new Dictionary<string, string>();
foreach (var global in paramObj.listGlobalParameter)
{
string columnValue = "";
var control = FindControl("GlobalParameters" + global.Name);
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "")
columnValue = txt.Text;
}
else if (control is DropDownList)
{
DropDownList lb = control as DropDownList;
if (lb.SelectedIndex != -1)
columnValue = lb.SelectedValue;
}
if (control is RadioButtonList)
{
RadioButtonList ct = control as RadioButtonList;
if (ct.SelectedIndex != -1)
columnValue = ct.SelectedValue;
}
globalParameters.Add(global.Name, columnValue);
}
return globalParameters;
}
private StringTable getColumnsAndValues(string groupName)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
List<AMLParam> listParam = paramObj.listInputParameter.Where(x => (x.Group == groupName)).ToList();
foreach (var param in listParam)
{
string columnValue = "";
var control = FindControl(groupName+param.Name);
if (control is TextBox)
{
TextBox txt = control as TextBox;
if (txt.Text != "")
columnValue = txt.Text;
}
else if (control is DropDownList)
{
DropDownList lb = control as DropDownList;
if (lb.SelectedIndex != -1)
columnValue = lb.SelectedValue;
}
if (control is RadioButtonList)
{
RadioButtonList ct = control as RadioButtonList;
if (ct.SelectedIndex != -1)
columnValue = ct.SelectedValue;
}
dict.Add(param.Name, columnValue);
}
string[] colums = new string[dict.Count];
dict.Keys.CopyTo(colums, 0);
string[] values = new string[dict.Count];
dict.Values.CopyTo(values, 0);
StringTable strtable = new StringTable();
strtable.ColumnNames = colums;
strtable.Values = new string[2, values.Length];
for (int i = 0; i < values.Length; i++)
{
strtable.Values[0, i] = values[i];
strtable.Values[1, i] = values[i];
}
return strtable;
}
//******************************************RRS Client code to submit request to server***************************************
//This is copied from the help page sample code and modified for ASP.NEt
async Task InvokeRequestResponseService_A(Dictionary<string, string> columnsDictionary)
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; });
//var strtable1 = getColumnsAndValues("input1");
var listInputs = new Dictionary<string, StringTable>();
foreach(var groupName in paramObj.listInputGroup)
{
StringTable strparam = getColumnsAndValues(groupName);
listInputs.Add(groupName, strparam);
}
var globalParameters = getGlobalParameters();
using (var client = new HttpClient())
{
var scoreRequest = new
{
Inputs = listInputs,
GlobalParameters = globalParameters
};
string apiKey = (paramObj.APIKey);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
client.BaseAddress = new Uri(webServicePostUrl);
// WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
// One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
// For instance, replace code such as:
// result = await DoSomeTask()
// with the following:
// result = await DoSomeTask().ConfigureAwait(false)
HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false); ;
if (response.IsSuccessStatusCode)
{
string apiResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
//List<string> listValues = ExtractValues(apiResult);
//GenerateControl.ShowOutput(OutputPlaceHolder, bottomsciprtPlaceHolde, paramObj.listOutputParameter, listValues, this);
List<OutputObject> listOutputObject = ExtractValuesObject(apiResult);
GenerateControl.ShowOutput(OutputPlaceHolder, bottomsciprtPlaceHolde, paramObj.listOutputParameter, listOutputObject, this);
FocusControlOnPageLoad("divResult");
}
else
{
//divResult.InnerText = string.Format("The request failed with status code: {0}", response.StatusCode);
//// Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
//divResult.InnerText = response.Headers.ToString();
string responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
OutputPlaceHolder.Controls.Add(new LiteralControl(responseContent));
//divResult.InnerText = responseContent;
}
}
}
static List<OutputObject> ExtractValuesObject(string jsonStr)
{
try
{
List<OutputObject> listOutput = new List<OutputObject>();
var objects = JObject.Parse(JObject.Parse(jsonStr)["Results"].ToString());
foreach (var output in objects)
{
OutputObject tmpOutput = new OutputObject();
tmpOutput.Name = output.Key;
try
{
JArray outputArray = JArray.Parse(output.Value["value"]["Values"][0].ToString());
foreach (var outputValue in outputArray)
tmpOutput.Values.Add(outputValue.ToString());
}
catch (Exception) { }
finally { listOutput.Add(tmpOutput); };
}
return listOutput;
}
catch (Exception)
{
return null;
}
}
public class StringTable
{
public string[] ColumnNames { get; set; }
public string[,] Values { get; set; }
}
public Dictionary<string, string> FeatureVector { get; set; }
public Dictionary<string, string> GlobalParameters { get; set; }
public class ScoreData
{
public Dictionary<string, string> FeatureVector { get; set; }
public Dictionary<string, string> GlobalParameters { get; set; }
}
public class ScoreRequest
{
public string Id { get; set; }
public ScoreData Instance { get; set; }
}
public void FocusControlOnPageLoad(string ClientID)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CtrlFocus", "ScrollView(\"" + ClientID +"\")", true);
}
}
}