title | description | date |
---|---|---|
In-proc VisualStudio.Extensibility extensions |
A reference for creating your first in-proc VisualStudio.Extensibility extension |
2022-07-13 |
While the VisualStudio.Extensibility model was created primarily to host extensions outside of the devenv.exe process, starting with Visual Studio 2022 17.4 Preview 1 it is possible to build a VisualStudio.Extensibility extension that are hosted within devenv.exe and can leverage traditional extensibility APIs provided by the Microsoft.VisualStudio.Sdk packages.
The support of in-proc extensions is meant to allow early adopters to leverage the new VisualStudio.Extensibility APIs while relying on Microsoft.VisualStudio.Sdk to cover any feature gap.
At this time, VSIX extensions containing VisualStudio.Extensibility references cannot be uploaded to the Visual Studio Marketplace due to VisualStudio.Extensibility being in preview status.
This document is a quick walkthrough on how to create your first in-proc extension using the VisualStudio.Extensibility model.
- Visual Studio 2022.5 Preview 1 or higher with
.Net desktop development
workload. The latest minimum requirement will always be listed at Announcements page. - Install VisualStudio.Extensibility Project System: This extension contains project templates for VisualStudio.Extensibility extensions.
- If you are updating from earlier builds, please make sure to update VisualStudio.Extensibility Project System to latest version as there are breaking changes in VisualStudio.Extensibility packages.
- Use the VisualStudio.Extensibility In-Process Extension Project template to create a new solution.
-
Set the Container project as Startup Project, press
F5
to start debugging. -
This will build your extension and deploy it to the experimental instance of Visual Studio version you are using. The debugger should attach once your extension is loaded.
-
You can find the command in
Tools
menu as shown.
An in-proc extension project references the Microsoft.VisualStudio.Sdk package which allows access to all Visual Studio SDK's services.
Traditionally, such services are consumed through either MEF or the AsyncServiceProvider. A VisualStudio.Extensibility extender is instead encouraged to leverage .NET depedency injection.
The MefInjection<TService>
and AsyncServiceProviderInjection<TService, TInterface>
classes (both from the Microsoft.VisualStudio.Extensibility.VSSdkCompatibility
namespace) allow to consume the Visual Studio SDK's services by simply adding them to the constructor of a class that is instantiated through dependency injection (like a command, tool window or extension part).
The example below shows how the DTE2
and IBufferTagAggregatorFactoryService
services can be added to a command.
[CommandIcon(KnownMonikers.Extension, IconSettings.IconAndText)]
[Command("MyFirstInProcExtension.Command1", "Sample Remote Command", placement: KnownCommandPlacement.ToolsMenu)]
public class Command1 : Command
{
private TraceSource traceSource;
private AsyncServiceProviderInjection<DTE, DTE2> dte;
private MefInjection<IBufferTagAggregatorFactoryService> bufferTagAggregatorFactoryService;
public Command1(
VisualStudioExtensibility extensibility,
TraceSource traceSource,
AsyncServiceProviderInjection<DTE, DTE2> dte,
MefInjection<IBufferTagAggregatorFactoryService> bufferTagAggregatorFactoryService,
string id)
: base(extensibility, id)
{
this.dte = dte;
this.bufferTagAggregatorFactoryService = bufferTagAggregatorFactoryService;
}
While using the VisualStudio.Extensibility In-Process Extension Project template takes care of setting up the entire solution, it is useful to know what are the basic components of an in-proc VisualStudio.Extensibility extension and how it differs from the out-of-proc variant described in the "create your first extension" guide.
An in-proc VisualStudio.Extensibility solution is composed of two projects:
- a class library that references both the VisualStudio.Extensibility and Visual Studio SDK packages and contains all the code of the extension,
- a container VSIX project that provides the ability to deploy the debug the extension.
This separation is a temporary solution while the VisualStudio.Extensibility is in preview and the final packaging and deployment design is being finalized.
The extender shouldn't add code, content or resources to the container project. The only goal of the container project is to include the assets provided by the other project.
Both the extension project and the container project must target the .NET version used by the target Visual Studio version. For Visual Studio 2022, they must target .NET Framework 4.7.2.
The Extension
class must be marked with the [HostingOptions(true)]
attribute that identifies the extension as being in-proc.
[HostingOptions(requiresInProcessHosting: true)]
public class InProcExtension : Extension
{