diff --git a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/ISkillTransport.cs b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/ISkillTransport.cs index ca7950fa74..4928d37358 100644 --- a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/ISkillTransport.cs +++ b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/ISkillTransport.cs @@ -6,7 +6,7 @@ namespace Microsoft.Bot.Builder.Skills { public interface ISkillTransport { - Task ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Func tokenRequestHandler = null); + Task ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Action tokenRequestHandler = null); Task CancelRemoteDialogsAsync(ITurnContext turnContext); diff --git a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillDialog.cs b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillDialog.cs index 0c7ff92acc..bfe06d219f 100644 --- a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillDialog.cs +++ b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillDialog.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Bot.Builder.Dialogs; @@ -26,6 +27,9 @@ public class SkillDialog : ComponentDialog private ISkillTransport _skillTransport; private Models.Manifest.Action _action; + private Queue _queuedResponses = new Queue(); + private object _lockObject = new object(); + /// /// Initializes a new instance of the class. /// SkillDialog constructor that accepts the manifest description of a Skill along with TelemetryClient for end to end telemetry. @@ -161,6 +165,13 @@ public override async Task EndDialogAsync(ITurnContext turnContext, DialogInstan var dialogResult = await ForwardToSkillAsync(innerDc, activity); + // if there's any response we need to send to the skill queued + // forward to skill and start a new turn + while (_queuedResponses.Count > 0) + { + await ForwardToSkillAsync(innerDc, _queuedResponses.Dequeue()); + } + _skillTransport.Disconnect(); return dialogResult; @@ -196,7 +207,7 @@ private async Task ForwardToSkillAsync(DialogContext innerDc, } } - private Func GetTokenRequestCallback(DialogContext dialogContext) + private Action GetTokenRequestCallback(DialogContext dialogContext) { return (activity) => { @@ -212,13 +223,12 @@ private Func GetTokenRequestCallback(DialogContext dialogCon tokenEvent.Name = TokenEvents.TokenResponseEventName; tokenEvent.Value = authResult.Result as ProviderTokenResponse; - return tokenEvent; - } - else - { - return null; + lock (_lockObject) + { + _queuedResponses.Enqueue(tokenEvent); + } } }; } } -} +} \ No newline at end of file diff --git a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillHttpTransport.cs b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillHttpTransport.cs index a580b67e9d..99f8a807da 100644 --- a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillHttpTransport.cs +++ b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillHttpTransport.cs @@ -45,7 +45,7 @@ public void Disconnect() // doesn't have to do any disconnect for http } - public async Task ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Func tokenRequestHandler = null) + public async Task ForwardToSkillAsync(ITurnContext dialogContext, Activity activity, Action tokenRequestHandler = null) { // Serialize the activity and POST to the Skill endpoint var httpRequest = new HttpRequestMessage(); @@ -83,11 +83,7 @@ public async Task ForwardToSkillAsync(ITurnContext dialogContext, Activity { if (tokenRequestHandler != null) { - var tokenResponseActivity = tokenRequestHandler(skillResponse); - if (tokenResponseActivity != null) - { - return await ForwardToSkillAsync(dialogContext, tokenResponseActivity); - } + tokenRequestHandler(skillResponse); } } else diff --git a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillWebSocketTransport.cs b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillWebSocketTransport.cs index fc57f31fd1..be8a611a6c 100644 --- a/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillWebSocketTransport.cs +++ b/lib/csharp/microsoft.bot.builder.skills/Microsoft.Bot.Builder.Skills/SkillWebSocketTransport.cs @@ -27,7 +27,7 @@ public SkillWebSocketTransport(SkillManifest skillManifest, MicrosoftAppCredenti _microsoftAppCredentialsEx = microsoftAppCredentialsEx ?? throw new ArgumentNullException(nameof(microsoftAppCredentialsEx)); } - public async Task ForwardToSkillAsync(ITurnContext turnContext, Activity activity, Func tokenRequestHandler = null) + public async Task ForwardToSkillAsync(ITurnContext turnContext, Activity activity, Action tokenRequestHandler = null) { if (_webSocketClient == null) { @@ -77,18 +77,13 @@ public void Disconnect() } } - private Action GetTokenCallback(ITurnContext turnContext, Func tokenRequestHandler) + private Action GetTokenCallback(ITurnContext turnContext, Action tokenRequestHandler) { - return async (activity) => + return (activity) => { if (tokenRequestHandler != null) { - var tokenResponseActivity = tokenRequestHandler(activity); - - if (tokenResponseActivity != null) - { - await ForwardToSkillAsync(turnContext, tokenResponseActivity); - } + tokenRequestHandler(activity); } }; }