Skip to content

Dependency Injection into Community Toolkit Visual Studio Extension ToolWindow is not working #531

@AvtsVivek

Description

@AvtsVivek

If I create a regular VSIX project(not a community one)

Basic Vs Extension Package

add a tool window,

Adding Simple Tool Window

and then add Autofac Dependency Injection infrastructure. This works fine as expected.

I basically picked up the idea from this article by Akos Nagy.

My full working example is here.

Note the tool window is derived from ToolWindowPane and it has a dependency on the UserControl as follows.

public class TrialToolWindow : ToolWindowPane
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TrialToolWindow"/> class.
    /// </summary>
    public TrialToolWindow(TrialToolWindowControl trialToolWindowControl) : base(null)
    {
        this.Caption = "TrialToolWindow";

        // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
        // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
        // the object returned by the Content property.
        this.Content = trialToolWindowControl;
    }
}

But then, I wanted to explore the same with community tool kit extension packages.

So I created a Community tool kit project with Toolwindow as follows. This is different from the above.

Community tool kit with tool window project

Note here the tool window derives from Community.VisualStudio.Toolkit.BaseToolWindow and looks as follows.

public class MyToolWindow : BaseToolWindow<MyToolWindow>
{

}

In this case again, the tool window has a dependency on User Control, and if it is hard coded(no DI) then it works fine.

Now I want to add Autofac infrastructure, and here is where the problem begins. The tool window does not show up. My project for this case is here

As tried to dig deeper into the BaseToolWindow class, I found that the problem is here when an object is newed up as follows.

_package = package;
_implementation = new T() { Package = package };

We know newing up stuff like that is a cardinal sin in DI, so I think is the problem.

But I am not sure the community development team did a mistake?

Or am I missing something here?

Is there some other/better way to do DI, that I am missing here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions