コード例 #1
0
        protected void AutoGenButton_Click(object sender, EventArgs e)
        {
            if (TextBoxConfigLoginAddress.Text == null || TextBoxConfigLoginAddress.Text == "")
            {
                LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                LabelGetServices.Text = "Please enter an address to the remote Configuration Service.";
                return;
            }
            try
            {
                Uri testConfigUri = new Uri(TextBoxConfigLoginAddress.Text);
            }
            catch (Exception)
            {
                LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                LabelGetServices.Text = "Please enter a valid address.  The address you entered is not a valid URI; no connection was attempted.";
                return;
            }
            string addressSes = null;
            string IDSes = null;
            ServiceUsers csUserSes = null;
            string confignameSes = null;
            string hosterSes = null;
            string versionSes = null;
            string platformSes = null;
            string clientSession = null;
            string nameSes = null;
            SessionInfo info = new SessionInfo();
            info.getSessionData(false, out addressSes, out csUserSes, out clientSession, out nameSes, out confignameSes, out hosterSes, out versionSes, out platformSes, out IDSes);
            address = addressSes;
            string currentbinding = clientSession;
            user = csUserSes;
            if (address == null || user == null)
                Response.Redirect(FormsAuthentication.LoginUrl,true);
            traversePath = DynamicTraversePath.getTraversePath(hostNameIdentifier, configName, ref configProxy, address, currentbinding, user);
            DropDownListServiceName.Items.Clear();
            string addressConfig = TextBoxConfigLoginAddress.Text.Trim();
            ClientInformation theClient = clients.Find(delegate(ClientInformation ciExist) { return ciExist.ElementName.Equals(DropDownListConfigClient.SelectedValue); });
            BindingInformation binding = bindings.Find(delegate(BindingInformation bindingItem) { return bindingItem.BindingConfigurationName.Equals(theClient.BindingConfiguration); });
            string selectedBinding = theClient.ElementName;
            if (binding != null && binding.BindingType != null)
            {
                switch (binding.BindingType)
                {
                    case ConfigUtility.BASIC_HTTP_BINDING:
                        {
                            if (!addressConfig.ToLower().StartsWith("http"))
                            {

                                LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                                LabelGetServices.Text = "You have selected an <b>Http</b> binding: Please enter a valid http address.";
                                return;
                            }
                            break;
                        }
                    case ConfigUtility.WS_HTTP_BINDING:
                        {
                            goto case ConfigUtility.BASIC_HTTP_BINDING;
                        }

                    case ConfigUtility.WS_2007_HTTP_BINDING:
                        {
                            goto case ConfigUtility.BASIC_HTTP_BINDING;
                        }



                    case ConfigUtility.NET_TCP_BINDING:
                        {
                            if (!addressConfig.ToLower().StartsWith("net.tcp"))
                            {
                                LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                                LabelGetServices.Text = "You have selected a <b>Tcp</b> binding: Please enter a valid tcp address in form of net.tcp://";
                                return;
                            }
                            break;
                        }
                }
            }
            ServiceUsers configUser = new ServiceUsers();
            configUser = user;
            csusername= TextBoxConfigLoginUserId.Text.ToLower().Trim();
            cspassword = TextBoxConfigLoginPassword.Text.Trim();
            ServiceConfigurationClient newConfigProxy = null;
            try
            {
                newConfigProxy = new ServiceConfigurationClient(currentbinding, address, configUser);
                configServices = newConfigProxy.getMyConfigServiceDetails(hostNameIdentifier, configName, traversePath, addressConfig, selectedBinding,csusername,cspassword, configUser);
                if (configServices != null)
                {
                    if (configServices.Count == 0)
                    {
                        LabelGetServices.Text = "The remote host is currently not returning any Configuration Service Hosts to connect to.";
                        LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                        return;
                    }
                    LabelGetServices.Text = "<span style=\"font-size:1.2em;color:palegreen\">Connected!</span>";
                    ViewState["configServices"] = configServices;
                }
                else
                {
                    LabelGetServices.Text = "Service refused to supply information. Check userid/password and try again.";
                    LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                    return;
                }
                HostNameID.Text = configServices[0].HostNameIdentifier;
                TextBoxConfigLoginUserId.Text = configServices[0].InitialCSUserID;
                TextBoxConfigLoginPassword.Text = configServices[0].InitialCSPassword;
                TextBoxConfigLoginPassword.Attributes.Add("value", configServices[0].InitialCSPassword);
                string hsContractSelected = null;
                List<ConnectedServices> csList = compositeServiceData[0].ConnectedServices;
                for (int i = 0; i < configServices[0].PrimaryHostedServices.Count; i++)
                {
                    if (configServices[0].PrimaryHostedServices[i].ServiceType == ConfigUtility.HOST_TYPE_PRIMARY || configServices[0].PrimaryHostedServices[i].ServiceType == ConfigUtility.HOST_TYPE_GENERIC)
                    {
                        ConnectedServices csCheck = csList.Find(delegate(ConnectedServices csExist) { return csExist.ServiceFriendlyName.Equals(configServices[0].PrimaryHostedServices[i].FriendlyName); });
                        if (csCheck == null)
                        {
                            if (hsContractSelected == null)
                                hsContractSelected = configServices[0].PrimaryHostedServices[i].FriendlyName;
                            DropDownListServiceName.Items.Add(new ListItem(configServices[0].PrimaryHostedServices[i].FriendlyName, configServices[0].PrimaryHostedServices[i].FriendlyName));
                        }
                        else
                        {
                            UpdateMessage.Text = "There are services you already have established definitions for, and these have been removed from the available list!";
                            UpdateMessage.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99");
                        }
                    }
                }
                if (DropDownListServiceName.Items.Count > 0)
                {
                    DropDownListServiceName.SelectedIndex = 0;
                    string serviceName = DropDownListServiceName.SelectedValue;
                    hsSelected = configServices[0].PrimaryHostedServices.Find(delegate(HostedServices hsExist) { return hsExist.FriendlyName.Equals(DropDownListServiceName.Items[0].Text); });
                    TextBoxOnlineMethod.Text = hsSelected.OnlineMethod;
                    TextBoxOnlineParms.Text = hsSelected.OnlineParms;
                    DropDownListServiceName.SelectedValue = hsContractSelected;
                    DropDownListServiceName.Text = hsContractSelected;
                    DropDownListServiceName.Enabled = true;
                    Add.Enabled = true;
                    LabelContract.Text = hsSelected.ServiceContract;
                    LabelServiceFriendlyName.Text = hsSelected.FriendlyName;
                    try
                    {
                        DropDownListPrimaryContract.SelectedValue = hsSelected.ServiceContract;
                    }
                    catch { }
                    LabelSvcBindingType.Text = hsSelected.BindingType;
                    LabelPort.Text = hsSelected.Port;
                    LabelUseHttps.Text = hsSelected.UseHttps.ToString();
                    LabelVPath.Text = hsSelected.VirtualPath;
                    SecurityMode.Text = hsSelected.SecurityMode;
                    DropDownListPrimaryClients.Items.Clear();
                    string thisHssecurityMode = hsSelected.SecurityMode;
                    string thisHsBindingType = hsSelected.BindingType;
                    for (int i = 0; i < clients.Count; i++)
                    {
                        if (DropDownListPrimaryContract.SelectedValue.Equals(clients[i].Contract))
                        {
                            BindingInformation theClientBinding = bindings.Find(delegate(BindingInformation biExist) { return biExist.BindingConfigurationName.Equals(clients[i].BindingConfiguration); });
                            if (theClientBinding != null)
                            {
                                string tsSecMode = null;
                                if (thisService == null)
                                    tsSecMode = "unknown";
                                else
                                    tsSecMode = thisService.SecurityMode;
                                if (theClientBinding.BindingType.Equals(thisHsBindingType) || thisHsBindingType.ToLower().Contains("custom"))
                                {
                                    if (theClientBinding.SecurityMode.Equals(tsSecMode) || tsSecMode.Equals("unknown"))
                                    {
                                        if (!clients[i].ElementName.ToLower().Contains("host") && !clients[i].ElementName.ToLower().Contains("client_configsvc") && !clients[i].ElementName.ToLower().Contains("client_nodesvc"))
                                        {
                                            DropDownListPrimaryClients.Items.Add(new ListItem(clients[i].ElementName, clients[i].ElementName));
                                            DropDownListPrimaryClients.SelectedValue = clients[i].ElementName;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    theClient = clients.Find(delegate(ClientInformation ciExist) { return ciExist.ElementName.Equals(DropDownListPrimaryClients.SelectedValue); });
                    if (theClient != null)
                    {
                        BindingInformation theSelectedBinding = bindings.Find(delegate(BindingInformation biExist) { return biExist.BindingConfigurationName.Equals(theClient.BindingConfiguration); });
                        if (theSelectedBinding != null)
                        {
                            LabelBindingTypePrimary.Text = theSelectedBinding.BindingType;
                        }
                        LabelIntroService.Text = "<div style=\"font-size:.9em;color:#FFFF99;padding-bottom:10px\">These are the available service endpoints by friendly names as assigned by the Service Hoster. Each has its own binding requirements " +
                            "(network scheme, security, encoding, etc.). Please select the desired service from this list. Client configurations and bindings can be generated by svcutil.exe, " + 
                            "named to Configuration Service standards, and inserted into this application's configuration file to appear in this page as selectable. You will also need to supply the service contract " +
                            "your client uses in your startup call per the StockTrader example and the documentation.<br/>";
                        Add.Enabled = true;
                    }
                    else
                    {
                        LabelIntroService.Text = "Please change the selected Primary Hosted Service, or the Client Contract.  The current combination yields no valid client configuration to connect with. The check was performed against binding type, and security mode. You may need to add and appropriate client definition to your config file.";
                        Add.Enabled = false;
                        LabelIntroService.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FFFF99");
                    }
                }
                else
                {
                    DropDownListServiceName.Enabled = false;
                    Add.Enabled = false;
                    LabelIntroService.Text = "Either this host does not expose services via the Config Service (try as a Generic Service); or you already have established definitions for all available services this virtual host exposes.";
                    LabelIntroService.ForeColor = System.Drawing.Color.Maroon;
                }
                LabelIntroPrimary.Text = "Please Select the Primary Service";
            }
            catch (Exception eConfig)
            {
                LabelGetServices.ForeColor = System.Drawing.Color.Maroon;
                LabelGetServices.Text = "Cannot Connect!<br/><br/>Please make sure the address to the remote configuration service endpoint is correct and you have chosen the appropriate client configuration/binding.  For example" +
                    " if specifying https, you must choose a client configuration using https/transport security.<br/>" +
                    "Full connect exception is: <br/>" + ConfigUtility.reMapExceptionForDisplay(eConfig.ToString());
                if (eConfig.InnerException != null)
                    LabelGetServices.Text = LabelGetServices.Text + "<br/> Inner Exception is:<br/>" + eConfig.InnerException.ToString();
                return;
            }
            generateClicked = "true";
            ViewState["generateClicked"] = generateClicked;
            ConnectedPanel.Visible = true;
            panelConnectedService1.Visible = true;
            panelConnectedService2.Visible = false;
        }