I'm working on a Visual Studio extension command in C# that should execute every time "Build" is executed. I made it a command so that it can manually be triggered, but I also want it too automatically trigger on "Build".
I was able to get it to work every time "Build" is executed using BuildEvents.OnBuildDone
event handler but for this to start executing every time I have to manually execute the command at least once the moment Visual Studio loads up.
I add my event handler to OnBuildDone
in the InitializeAsync
function (where the initialization code goes) which executes after manually executing command (which is the issue). Don't know where else to put it.
I looked at Visual Studio extensions in the marketplace to see how to execute logic on build and it worked but only when pressing the command manually first. Also read a bunch of documentation and posts.
To replicate what I'm trying to do:
Its basically this small tutorial:
which requires installing Visual Studio SDK through Visual Studio installer
Only real difference is that for the Execute function I have my own logic. But that's not important. I'm trying to make the execute logic execute after every build from the moment visual studio loads with my extension. What I did was use the OnBuildDone event handler in the InitializeAsync function in CommandExtensionPackage.cs class. However, this only executes when command is clicked manually. I'm trying to immediantly without it being reliant on having to click on the command.
I'm working on a Visual Studio extension command in C# that should execute every time "Build" is executed. I made it a command so that it can manually be triggered, but I also want it too automatically trigger on "Build".
I was able to get it to work every time "Build" is executed using BuildEvents.OnBuildDone
event handler but for this to start executing every time I have to manually execute the command at least once the moment Visual Studio loads up.
I add my event handler to OnBuildDone
in the InitializeAsync
function (where the initialization code goes) which executes after manually executing command (which is the issue). Don't know where else to put it.
I looked at Visual Studio extensions in the marketplace to see how to execute logic on build and it worked but only when pressing the command manually first. Also read a bunch of documentation and posts.
To replicate what I'm trying to do:
Its basically this small tutorial: https://learn.microsoft/en-us/visualstudio/extensibility/extensibility-hello-world?view=vs-2022
which requires installing Visual Studio SDK through Visual Studio installer
Only real difference is that for the Execute function I have my own logic. But that's not important. I'm trying to make the execute logic execute after every build from the moment visual studio loads with my extension. What I did was use the OnBuildDone event handler in the InitializeAsync function in CommandExtensionPackage.cs class. However, this only executes when command is clicked manually. I'm trying to immediantly without it being reliant on having to click on the command.
Share Improve this question edited Apr 1 at 18:28 Vinnie asked Mar 31 at 13:49 VinnieVinnie 32 bronze badges New contributor Vinnie is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 5- Maybe you could initialize your extension by handling a solution load event. – Olivier Jacot-Descombes Commented Mar 31 at 15:05
- Where would I have the code for initializing my extension? Would it be in InitializeAsync function? If so, the issue with this is it seems to execute when command is manually pressed on rather than Visual Studio being loaded up. (Tested this out by putting a breakpoint in the function to see when it executes). – Vinnie Commented Mar 31 at 18:36
- Can you post the class that's used for your extension? – Charles Henington Commented Apr 1 at 5:20
- @Vinnie Could you please share a minimal and reproducible example to help us better understand your scenario? – Dou Xu-MSFT Commented Apr 1 at 6:08
- Just edited my question to provide more information how to recreate. – Vinnie Commented Apr 1 at 18:46
1 Answer
Reset to default 1From looking at this document How can I run my add-in code in a VSPackage?
Add-in code usually runs in one of two ways:
- Triggered by a menu command (the code is in the IDTCommandTarget.Exec method)
- Automatically on startup (the code is in the OnConnection event handler.)
You can add attribute ProvideAutoLoad
to Package class, this will load the extension when a solution is opened.
Docs Referred:
To autoload a VSPackage