I have a dynamic menu button in our custom VSTO ribbon. I am loading content to it at runtime. That process is working fine. Dynamic drop down is creating fine. this is the code.
In Ribbon.xml
<dynamicMenu id="RunWorkflow" size="large" getLabel="GetLabel" getImage="GetImage" getScreentip="GetScreenTip" getSupertip="GetSuperTip" keytip="EJ" getContent="GetWorkflows"/>
In Ribbon.cs
public string GetWorkflows(Office.IRibbonControl control)
{
string menuItem = "<menu xmlns=\"http://schemas.microsoft.com/office/2009/07/customui\">";
List<string> workflows = FetchWorkflows(); //there were few lines above for fetch workflows. This part works fine.
if (workflows != null && workflows.Count > 0)
{
int count = 1;
foreach (string workflowPath in workflows)
{
string[] paths = workflowPath.Split('/');
var name = paths[paths.Count() - 1].Replace(".abc_workflow", "");
var id = "Workflows" + count.ToString();
menuItem = menuItem + "<button id=\"" + id + "\" label=\"" + name + "\" onAction=\"TPS_WorkflowProcessing\" getImage=\"GetImage\" tag=\"" + workflowPath + "\"/>";
count++;
}
menuItem = menuItem + "</menu>";
}
return menuItem;
}
public void TPS_WorkflowProcessing(Office.IRibbonControl control)
{
string workflowPath = control.Tag;
using (MyApplication app = new MyApplication())
{
app.Tools.RunDocumentProcessing(workflowPath);
}
}
On Tools.cs
public void RunDocumentProcessing(string workflowPath)
{
if (this.IsActiveDocumentReady)
{
string outputFileExtesion = Constants.EXTENTION_DOCX;
this.RunJob(workflowPath, outputFileExtesion);
}
}
private async void RunJob(string workflowPath, string outputExtension)
{
//Noting from inside is getting executed
}
In the RunJob() function, I am sending multiple async server requests. So I have to make it async.
This code executes fine when run via Visual Studio. But when we build the installer and run with it, it is executing up to string outputFileExtesion = Constants.EXTENTION_DOCX; line on Tools.RunDocumentProcessing function. The RunJob() is not reaching. Why is that? How to fix this?
using (MyApplication app = new MyApplication())will dispose app as soon asRunJobhands control back to the caller, which is when it hits the firstawait. Could this be the problem? You probably need to make everything async up toGetWorkflows.using (MyApplication app = new MyApplication())looks kind of suspicious to me in itself, to start with.private async void RunJob(string workflowPath, string outputExtension)this cannot be awaited. As a rule of thumb:async voidis OK for event handlers, only (with very very few exceptions). Which means, I share Klaus' view on this - it needs to be "async all the way".