예제 #1
0
        private void butCompare_Click(object sender, EventArgs e)
        {
            // Check for empty passwords if security is enabled
            if (_securityEnabled && !PasswordIsValid())
            {
                return;
            }

            // Disable the UI while we run the comparison
            groupUserCredentials.Enabled = false;
            groupDocSelection.Enabled    = false;
            groupOutput.Enabled          = false;

            this.Cursor = Cursors.WaitCursor;


            // This code demonstrates how to call the BasicHttp legacy service
            // from a Service Reference proxy (This protocol may still be imported
            // as a Web Service Reference if required for legacy language support.

            /*
             * CompareProxy.LegacyComparerClient asmxCompare = new Document.Services.Compare.Sample.CompareProxy.LegacyComparerClient();
             *
             * if( asmxCompare.Authenticate("DIS", "User", "Pass") )
             * {
             *      CompareProxy.CompareResult cr = asmxCompare.Execute2(   System.IO.File.ReadAllBytes(textBoxOriginal.Text),
             *                                                                                                                      System.IO.File.ReadAllBytes(textBoxModified.Text),
             *                                                                                                                      CompareProxy.CompareResponseFlags.Rtf,
             *                                                                                                                      "");
             * }
             */


            // This code demonstrates connecting to the Compare service via the WSHttp binding
            // which ensures more secure transport and data compression.
            CompareProxy.ComparerClient svcCompare = new CompareProxy.ComparerClient("CompareWebServiceWCF", textHost.Text);


            if (_securityEnabled)
            {
                svcCompare.ClientCredentials.Windows.ClientCredential.UserName = textUsername.Text;
                svcCompare.ClientCredentials.Windows.ClientCredential.Password = textPassword.Text;
                svcCompare.ClientCredentials.Windows.ClientCredential.Domain   = textDomain.Text;
                //svcCompare.ClientCredentials.Windows.AllowNtlm = true;
                //svcCompare.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
            }

            try
            {
                // Always call Authenticate as the first operation and use the same Client Credentials
                bool isAuthenticated = !_securityEnabled || (_securityEnabled &&
                                                             svcCompare.Authenticate(textDomain.Text, textUsername.Text, textPassword.Text));

                if (isAuthenticated)
                {
                    // Determine which option was specified for the
                    // result of the comparison.  RTF, XML, WDF etc.
                    CompareProxy.ResponseOptions desiredResults = 0;

                    if (checkWDF.Checked)
                    {
                        desiredResults = CompareProxy.ResponseOptions.Wdf;
                    }
                    else if (checkRedline.Checked)
                    {
                        //cboConvert index
                        // 0 = RTF
                        // 1 = Doc
                        // 2 = DocX
                        // 3 = Pdf
                        switch (cboConvert.SelectedIndex)
                        {
                        case 0:
                            desiredResults = CompareProxy.ResponseOptions.Rtf;
                            break;

                        case 1:
                            desiredResults = CompareProxy.ResponseOptions.Doc;
                            break;

                        case 2:
                            desiredResults = CompareProxy.ResponseOptions.DocX;
                            break;

                        case 3:
                            desiredResults = CompareProxy.ResponseOptions.Pdf;
                            break;
                        }
                    }


                    if (checkSummary.Checked)
                    {
                        desiredResults |= CompareProxy.ResponseOptions.Xml;
                    }

                    if (checkRedlinMl.Checked)
                    {
                        desiredResults |= CompareProxy.ResponseOptions.RedlinMl;
                    }

                    // Run the comparison
                    var originalInfo = new DocumentInfo()
                    {
                        DocumentDescription = Path.GetFileName(textBoxOriginal.Text),
                        DocumentSource      = textBoxOriginal.Text
                    };
                    var modifiedInfo = new DocumentInfo()
                    {
                        DocumentDescription = Path.GetFileName(textBoxModified.Text),
                        DocumentSource      = textBoxModified.Text
                    };
                    var executeParams = new ExecuteParams
                    {
                        Modified             = System.IO.File.ReadAllBytes(textBoxModified.Text),
                        Original             = System.IO.File.ReadAllBytes(textBoxOriginal.Text),
                        ResponseOption       = desiredResults,
                        OriginalDocumentInfo = originalInfo,
                        ModifiedDocumentInfo = modifiedInfo
                    };


                    // If a server-side option set has been specifed then call SetOptionsSet
                    if (checkUseDefaultOptionsSet.Checked)
                    {
                        if (comboOptionsSets.SelectedIndex != 0)
                        {
                            if (svcCompare.SetOptionsSet(comboOptionsSets.Text))
                            {// The option set is defined on the server
                                if (!_securityEnabled)
                                {
                                    //As Sessions are not supported with basic Http binding,
                                    //the name of the server-side option must be specified
                                    executeParams.CompareOptionInfo = comboOptionsSets.Text;
                                }
                            }                             // else This is not an existing server-side option set
                        }

                        //To use a server-side options set just set the options string to "".
                        executeParams.CompareOptions = string.Empty;
                    }
                    else
                    {
                        // If using client-side options set then load
                        // the options from the specified file.
                        executeParams.CompareOptions    = GetCompareOptions();
                        executeParams.CompareOptionInfo = textBoxOptionSet.Text;
                    }



                    CompareResults cr = svcCompare.ExecuteEx(executeParams);

                    //Comparison without documents details
//					CompareProxy.CompareResults cr = svcCompare.Execute(System.IO.File.ReadAllBytes(textBoxOriginal.Text),
//																		System.IO.File.ReadAllBytes(textBoxModified.Text),
//																		desiredResults,
//																		sCompareOptions);



                    // Always close the service connection as soon as possible after use.
                    svcCompare.Close();

                    if (cr != null)
                    {
                        // Write out the XML summary and display with the
                        // default application for .xml
                        if ((desiredResults & CompareProxy.ResponseOptions.Xml) == CompareProxy.ResponseOptions.Xml)
                        {
                            if (cr.Summary != null)
                            {
                                System.IO.File.WriteAllText(textSummary.Text, cr.Summary);
                                System.Diagnostics.Process.Start(textSummary.Text);
                            }
                            else
                            {
                                MessageBox.Show("Execute did not return a summary", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }

                        if (checkRedlinMl.Checked)
                        {
                            if (cr.RedlineMl != null)
                            {
                                System.IO.File.WriteAllText(textRedlineMl.Text, cr.RedlineMl);
                                System.Diagnostics.Process.Start("Notepad.exe", textRedlineMl.Text);
                            }
                            else
                            {
                                MessageBox.Show("Execute did not return a redline ML", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        // Write out the RTF redline, if selected, and display with the
                        // internal viewer
                        if ((desiredResults & CompareProxy.ResponseOptions.Rtf) == CompareProxy.ResponseOptions.Rtf)
                        {
                            if (cr.Redline != null)
                            {
                                string rtfRedline = FromASCIIByteArray(cr.Redline);

                                formCompareResults formResults = new formCompareResults();
                                formResults.SetRTFResults(rtfRedline);

                                formResults.ShowDialog(this);

                                System.IO.File.WriteAllBytes(textRedline.Text, cr.Redline);
                                //System.Diagnostics.Process.Start(textRedline.Text);
                            }
                            else
                            {
                                MessageBox.Show("Execute did not return a redline", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }                        // Write out the WDF composite and display with the default application for .wdf
                        else if ((desiredResults & CompareProxy.ResponseOptions.Wdf) == CompareProxy.ResponseOptions.Wdf)
                        {
                            if (cr.Redline != null)
                            {
                                System.IO.File.WriteAllBytes(textWDF.Text, cr.Redline);
                                System.Diagnostics.Process.Start(textWDF.Text);
                            }
                            else
                            {
                                MessageBox.Show("Execute did not return a redline", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        // Check if we want a different redline convertor then the standard RTF
                        else if ((desiredResults & CompareProxy.ResponseOptions.Doc) == CompareProxy.ResponseOptions.Doc ||
                                 (desiredResults & CompareProxy.ResponseOptions.DocX) == CompareProxy.ResponseOptions.DocX ||
                                 (desiredResults & CompareProxy.ResponseOptions.Pdf) == CompareProxy.ResponseOptions.Pdf)
                        {
                            if (cr.Redline != null)
                            {
                                System.IO.File.WriteAllBytes(textRedline.Text, cr.Redline);
                                System.Diagnostics.Process.Start(textRedline.Text);
                                MessageBox.Show(this, string.Format(CultureInfo.CurrentCulture, "Written results to {0}", textRedline.Text),
                                                "Results", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                            {
                                MessageBox.Show("Execute did not return a redline", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Execute did not return any results", "Compare Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (System.ServiceModel.ServerTooBusyException)
            {
                MessageBox.Show("Server Too Busy", "Compare", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (TimeoutException ex)
            {
                MessageBox.Show(ex.Message, "TimeoutException", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.ServiceModel.FaultException ex)
            {
                //svcCompare.Abort();
                MessageBox.Show(ex.Message, "FaultException", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                //svcCompare.Abort();

                if (ex.Message.Contains("request is unauthorized"))
                {
                    MessageBox.Show(@"Unauthorized issue arised, please check User Name and/or Password and/or domain!",
                                    @"CommunicationException",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(ex.Message, "CommunicationException", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                // When attempting to open the comparison results it is possible
                // that no document is associated with the file extension.
                if (ex.NativeErrorCode == 0x00000483)
                {
                    MessageBox.Show("The redline has been saved to the specified location but no application has been associated with this file type in order to display it.", "File Associations", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                //svcCompare.Close();
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Re-enable UI
            groupOutput.Enabled          = true;
            groupDocSelection.Enabled    = true;
            groupUserCredentials.Enabled = true;

            this.Cursor = Cursors.Default;
        }