2
所以我有我打電話,像這樣這個對話框鏈...麻煩與對話鏈
public static readonly IDialog<string> dialog = Chain.PostToChain()
.Select(msg => msg.Text)
.Switch(
new RegexCase<IDialog<string>>(new Regex("^hi", RegexOptions.IgnoreCase), (context, txt) =>
{
return Chain.ContinueWith(new GreetingDialog(),
async (ctx, res) =>
{
var token = await res;
var name = "User";
context.UserData.TryGetValue<string>("Name", out name);
return Chain.Return($"You are logged in as: {name}");
});
}),
new DefaultCase<string, IDialog<string>>((context, txt) =>
{
int count;
context.UserData.TryGetValue("count", out count);
context.UserData.SetValue("count", ++count);
string reply = string.Format("{0}: You said {1}", count, txt);
return Chain.Return(reply);
}))
.Unwrap()
.PostToUser();
對話框看起來像這樣
[Serializable]
public class GreetingDialog : IDialog
{
public async Task StartAsync(IDialogContext context)
{
await context.PostAsync("Hi I'm John Bot");
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var message = await argument;
var userName = String.Empty;
var getName = false;
context.UserData.TryGetValue<string>("Name", out userName);
context.UserData.TryGetValue<bool>("GetName", out getName);
if (getName)
{
userName = message.Text;
context.UserData.SetValue<string>("Name", userName);
context.UserData.SetValue<bool>("GetName", false);
}
if (string.IsNullOrEmpty(userName))
{
await context.PostAsync("What is your name?");
context.UserData.SetValue<bool>("GetName", true);
}
else
{
await context.PostAsync(String.Format("Hi {0}. How can I help you today?", userName));
}
context.Wait(MessageReceivedAsync);
}
}
,當我嘗試運行此我得到回來,說錯誤「:Microsoft.Bot.Builder.Internals.Fibers.ClosureCaptureException:異常,捕捉環境匿名方法閉包是不可序列,考慮取消環境捕獲或使用反射序列化代理」
有人能指出我在正確的方向?我不確定我在這裏做錯了什麼。
OMG太感謝你了!這就是訣竅! – Matt