public override NSObject OpenDocument(NSUrl url, bool displayDocument, out NSError outError)
        {
            outError = AppDelegate.SuppressionNSError;

            if (CommandLineTool.TestDriver.ShouldRun)
            {
                return(null);
            }

            SessionDocument document = null;

            try {
                ClientSessionUri sessionUri;

                try {
                    sessionUri = (ClientSessionUri) new Uri(url.ToString());
                } catch (Exception e) {
                    throw new UriFormatException(Catalog.GetString(
                                                     "Invalid or unsupported URI."), e);
                }

                if (clientSessionController.TryGetApplicationState(
                        sessionUri,
                        out document) && document != null)
                {
                    document.MakeKeyAndOrderFront(this);
                    return(document);
                }

                document = base.OpenDocument(url, displayDocument, out outError) as SessionDocument;

                if (outError != null && outError != AppDelegate.SuppressionNSError)
                {
                    throw new NSErrorException(outError);
                }

                if (document != null)
                {
                    clientSessionController.AddSession(document.Session, document);
                }

                return(document);
            } catch (Exception e) {
                if (document != null)
                {
                    try {
                        document.Close();
                    } catch (Exception ee) {
                        Log.Error(TAG, "Exception caught when trying to close document " +
                                  "due to exception when opening document", ee);
                    }
                }

                e.ToUserPresentable($"“{url}” could not be opened.").Present();
            }

            return(null);
        }
        public void CloseDocument(SessionDocument document)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (document.Session != null)
            {
                document.Session.Dispose();
                clientSessionController.RemoveSession(document.Session);
            }
        }