Search code examples
visual-studio-extensionsvsixvsxvs-extensibility

Why does exception in DocData.LoadDocData does not prevent the view from opening?


I am struggling with this one.

I have a Visual Studio package that registers a custom editor factory to create a custom doc data and custom doc view.

In the DocData, in the LoadDocData method, when I create the document, if the file being opened is corrupt a InvalidOperationException is raised.

The problem here is that I don't want the corresponding view to be opened but Visual Studio shows an error message but it still opens the view.

What is wrong here?

protected override int LoadDocData(string fileName, bool isReload)
{
    // Clear errors

    this.DocumentData.ClearErrorListItems();

    // Catch errors

    try
    {
        base.LoadDocData(fileName, isReload); // InvalidOperationException raised here
    }
    catch (Exception)
    {
        if (this.DocumentData.ErrorListProvider != null)
        {
            this.DocumentData.ErrorListProvider.ShowErrorOnIdle();
        }

        throw;
    }

    return VSConstants.S_OK;
}

Thanks for any help!


Solution

  • The issue here was that my custom doc view was not a subclass of ModelingDocView.

    I found, by analyzing the source code in DocData.LoadDocData with ILSpy, that it only closes the views that derive from ModelingDocView.

    public int LoadDocData(string fileName)
    {
        this.codeMarkers.CodeMarker(8206);
        this.OnDocumentLoading(EventArgs.Empty);
        try
        {
            this.LoadDocData(fileName, false);
        }
        catch (Exception ex)
        {
            if (CriticalException.IsCriticalException(ex))
            {
                throw;
            }
            this.HandleLoadDocDataException(fileName, ex, false);
            foreach (ModelingDocView current in new List<ModelingDocView>(this.DocViews))
            {
                if (current != null)
                {
                    ErrorHandler.ThrowOnFailure(current.Frame.CloseFrame(65792u));
                }
            }
            return 0;
        }
        this.loaded = true;
        this.OnDocumentLoaded(EventArgs.Empty);
        this.codeMarkers.CodeMarker(8207);
        return 0;
    }