private void ReportErrorToUser(ApplicationUnhandledExceptionEventArgs e, string errorMessage)
        {
            if (e.ExceptionObject != null)
            {
                errorDisplay = new ErrorDisplay();

                // Attempt to get the stack trace with IL offsets
                string stackTraceIL = e.ExceptionObject.StackTraceIL();

                ErrorData data = new ErrorData()
                {
                    Message = errorMessage ?? e.ExceptionObject.Message,
                    StackTrace = !string.IsNullOrEmpty(stackTraceIL) ? stackTraceIL :
                        e.ExceptionObject.StackTrace
                };

                errorDisplay.DataContext = data;

                if (Application.Current.RootVisual != null)
                {
                    // Size the error UI
                    double width = Application.Current.RootVisual.RenderSize.Width * 0.67;
                    errorDisplay.Width = width > errorDisplay.MaxWidth ? errorDisplay.MaxWidth : width;
                    errorDisplay.Completed += new EventHandler<EventArgs>(errorDisplay_Completed);

                    // Show the error
                    BuilderApplication.Instance.ShowWindow(Strings.ErrorOccured, errorDisplay, false, null, null);
                }
                else
                {
                    MessageBox.Show(data.Message + "\n" + data.StackTrace, Strings.ErrorOccured, MessageBoxButton.OK);
                }
            }
        }
        private void ReportErrorToUser(ApplicationUnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject != null)
            {
                errorDisplay = new ErrorDisplay();

                // Attempt to get the stack trace with IL offsets
                string stackTraceIL = e.ExceptionObject.StackTraceIL();

                ErrorData data = new ErrorData()
                {
                    Message = e.ExceptionObject.Message,
                    StackTrace = !string.IsNullOrEmpty(stackTraceIL) ? stackTraceIL : 
                        e.ExceptionObject.StackTrace
                };

                errorDisplay.DataContext = data;

                // Size the error UI
                double width = Application.Current.RootVisual.RenderSize.Width * 0.67;
                errorDisplay.Width = width > errorDisplay.MaxWidth ? errorDisplay.MaxWidth : width;
                errorDisplay.Completed += new EventHandler<EventArgs>(errorDisplay_Completed);

                // Show the error
                MapApplication.Current.ShowWindow(StringResourcesManager.Instance.Get("ErrorCaption"), 
                    errorDisplay);
            }
        }
        public void UploadExtensionLibraryAsync(string selectedFileName, byte[] fileContents, ObservableCollection<string> assemblies, object userState)
        {
            Uri uri = CreateRestRequest("Extensions/Upload");

            var assembliesString = assemblies != null ? string.Join(",", assemblies) : null;
            var content = new MultipartFormDataContent();
            var ms = new MemoryStream(fileContents);
            var fileContent = new StreamContent(ms);

            // Specify the content disposition and content type - without this the form data will not
            // be included in the Request object in .NET 2.0 app pools
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"files\"",
                FileName = "\"" + selectedFileName + "\""
            };
            fileContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-silverlight-app");
            content.Add(fileContent);
            
            var stringContent = new StringContent(assembliesString);

            // Need to specify the content disposition and content type for .NET 2.0 compatibility here, too
            stringContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                Name = "\"assemblies\""
            };
            stringContent.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
            content.Add(stringContent);

            var client = new HttpClient();
            client.PostAsync(uri, content).ContinueWith(t =>
            {
                stringContent.Dispose();
                fileContent.Dispose();
                ms.Dispose();
                content.Dispose();

                if (t.IsCanceled)
                    return;

                if (t.Exception != null)
                {
                    var errorDisplay = new ErrorDisplay();

                    // Attempt to get the stack trace with IL offsets
                    string stackTraceIL = t.Exception.StackTraceIL();

                    ErrorData data = new ErrorData()
                    {
                        Message = t.Exception.Message,
                        StackTrace = !string.IsNullOrEmpty(stackTraceIL) ? stackTraceIL :
                            t.Exception.StackTrace
                    };

                    errorDisplay.DataContext = data;

                    // Size the error UI
                    double width = Application.Current.RootVisual.RenderSize.Width * 0.67;
                    errorDisplay.Width = width > errorDisplay.MaxWidth ? errorDisplay.MaxWidth : width;
                    errorDisplay.Completed += (o, a) => BuilderApplication.Instance.HideWindow(errorDisplay);

                    // Show the error
                    BuilderApplication.Instance.ShowWindow(Strings.ErrorOccured, errorDisplay, false, null, null);
                }

                if (UploadExtensionLibraryCompleted != null)
                {
                    UploadExtensionLibraryCompleted(this, new UploadExtensionLibraryCompletedEventArgs()
                    {
                        Error = t.Exception,
                        UserState = userState,
                    });
                }

            }, TaskScheduler.FromCurrentSynchronizationContext());
        }