예제 #1
0
파일: MainForm.cs 프로젝트: mmaxner/SOA-A2
        /// <summary>
        ///     Creates a set of controls for manipulating arguments to be sent in a SOAP call, based on the CurrentAction.
        ///     Attaches thse controls to grpArgumentControls, after clearing existing controls from it.
        /// </summary>
        public void GenerateArgumentControls()
        {
            try
            {
                grpArgumentControls.Controls.Clear();
                for (int p = 0; p < CurrentAction.Parameters.Count; p++)
                {
                    Label label    = new Label();
                    Point location = ARGUMENT_LABEL_LOCATION;
                    for (int j = 0; j < p; j++)
                    {
                        location += ARGUMENT_OFFSET;
                    }
                    label.Location = location;
                    label.Size     = ARGUMENT_LABEL_SIZE;
                    label.Text     = string.Format("{0} ({1})", CurrentAction.Parameters[p].UIName, CurrentAction.Parameters[p].DataType);

                    Control control = null;

                    // based on the type of parameter, create and data bind a control for the user to enter a value with
                    switch (CurrentAction.Parameters[p].DataType)
                    {
                    // lists are displayed in a combobox
                    // the items in the combo box are populated by another action, detailed in the list source of the Current Argument
                    case DATA_TYPE_IDENTIFIER_LIST:
                        ComboBox listPicker = new ComboBox();

                        // retrieve the list of items from the service
                        string XMLList = WebServiceFramework.CallWebService(
                            CurrentService.URL,
                            CurrentAction.Parameters[p].ListSource.ServiceName,
                            new List <SOAPViewerConfig.SOAPParameter>(),
                            CurrentService.NameSpace);

                        // create a binding source, find the values for display/data
                        BindingSource          bindingSource1 = new BindingSource();
                        XDocument              docList        = XDocument.Parse(XMLList);
                        IEnumerable <XElement> dataList       = docList.Descendants().Where(x => x.Name.LocalName == CurrentAction.Parameters[p].ListSource.DataMember);
                        IEnumerable <XElement> displayList    = docList.Descendants().Where(x => x.Name.LocalName == CurrentAction.Parameters[p].ListSource.DisplayMember);

                        // store the list in a dictionary
                        Dictionary <string, string> dataSource = new Dictionary <string, string>();
                        for (int j = 0; j < dataList.Count(); j++)
                        {
                            dataSource.Add(dataList.ElementAt(j).Value
                                           , displayList.ElementAt(j).Value);
                        }

                        // set the data source to the dictionary, and bind to the CurrentArgument
                        bindingSource1.DataSource = dataSource;
                        listPicker.DataSource     = bindingSource1;
                        listPicker.DisplayMember  = "Value";
                        listPicker.ValueMember    = "Key";

                        listPicker.DataBindings.Add("SelectedValue", CurrentAction.Parameters[p], "Value");

                        control = listPicker;
                        break;

                    case DATA_TYPE_IDENTIFIER_DATETIME:
                        DateTimePicker datePicker = new DateTimePicker();
                        datePicker.Format                 = DateTimePickerFormat.Custom;
                        datePicker.CustomFormat           = "yyyy-MM-dd";
                        CurrentAction.Parameters[p].Value = DateTime.Now.ToString("yyyy-MM-dd");

                        Binding binding = new Binding("Value", CurrentAction.Parameters[p], "Value", true);

                        datePicker.DataBindings.Add(binding);

                        control = datePicker;
                        break;

                    case DATA_TYPE_IDENTIFIER_INT:
                        NumericUpDown numberPicker = new NumericUpDown();
                        numberPicker.Minimum = int.MinValue;
                        numberPicker.Maximum = int.MaxValue;
                        CurrentAction.Parameters[p].Value = "0";

                        numberPicker.DataBindings.Add("Value", CurrentAction.Parameters[p], "Value", true, DataSourceUpdateMode.OnPropertyChanged);
                        control = numberPicker;
                        break;

                    case DATA_TYPE_IDENTIFIER_STRING:
                    default:
                        TextBox textBox = new TextBox();
                        textBox.DataBindings.Add("Text", CurrentAction.Parameters[p], "Value");
                        control = textBox;
                        break;
                    }

                    // set the location of the control so that multiple controls don't overlap
                    control.Location = location + ARGUMENT_TEXTBOX_OFFSET;
                    control.Size     = ARGUMENT_TEXTBOX_SIZE;
                    // add the control to the group box
                    grpArgumentControls.Controls.Add(label);
                    grpArgumentControls.Controls.Add(control);
                }
            }
            catch (System.Net.WebException ex)
            {
                MessageBox.Show(this, ex.Message, ERROR_CONNECTING_TO_SERVICE, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, ERROR_DEFAULT, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
예제 #2
0
파일: MainForm.cs 프로젝트: mmaxner/SOA-A2
        /// <summary>
        ///     Sends a SOAP request when the Invoke button is pressed.
        ///     The results are parsed, and displayed in the bottom text-area
        /// </summary>
        private async void btnInvoke_ClickAsync(object sender, EventArgs e)
        {
            try
            {
                string errorMessage   = string.Empty;
                bool   isAllValidData = true;

                // validate the arguments
                foreach (SOAPViewerConfig.SOAPParameter argument in CurrentAction.Parameters)
                {
                    errorMessage = isValidData(argument);
                    if (!string.IsNullOrEmpty(errorMessage))
                    {
                        isAllValidData = false;
                        break;
                    }
                }

                if (isAllValidData)
                {
                    // make the SOAP request
                    string result = WebServiceFramework.CallWebService(CurrentService.URL, CurrentAction.Name, CurrentAction.Parameters, CurrentService.NameSpace);
                    // put the appropriate text the text area
                    txtResults.Clear();
                    if (!string.IsNullOrEmpty(result))
                    {
                        using (Stream resultStream = GenerateStreamFromString(result))
                        {
                            await parseAndDisplayResponse(resultStream);
                        }
                    }
                    else
                    {
                        txtResults.AppendText(NO_RESULT_MESSAGE);
                    }
                }
                else
                {
                    MessageBox.Show(this, errorMessage, ERROR_PARSING_ARGUMENT, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (System.Net.WebException ex)
            {
                string message = ex.Message;
                string caption = ERROR_CONNECTING_TO_SERVICE;
                if (ex.Response != null)
                {
                    string response;
                    using (StreamReader responseReader = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        response = responseReader.ReadToEnd();
                    }
                    XDocument xdoc = XDocument.Parse(response);
                    if (xdoc.Descendants("faultstring").Count() > 0)
                    {
                        message = xdoc.Descendants("faultstring").First().Value;
                        caption = SOAP_FAULT;
                    }
                }
                MessageBox.Show(this, message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, ERROR_DEFAULT, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }