Skip to content

Commit e3f48fe

Browse files
committed
Add error resilient context store
1 parent 26656d9 commit e3f48fe

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

CSharp/Library/Dialogs/Conversations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ internal static BinaryFormatter MakeBinaryFormatter(IServiceProvider provider)
9797
};
9898
var formatter = Conversation.MakeBinaryFormatter(provider);
9999

100-
IDialogContextStore store = new DialogContextStore(formatter);
100+
IDialogContextStore contextStore = new DialogContextStore(formatter);
101+
IDialogContextStore store = new ErrorResilientDialogContextStore(contextStore);
101102

102103
IDialogContext context;
103104
if (!store.TryLoad(botData.PerUserInConversationData, BlobKey, out context))

CSharp/Library/Dialogs/DialogContextStore.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,33 @@ void IDialogContextStore.Save(IDialogContext context, IBotDataBag bag, string ke
8888
bag.SetValue(key, blobNew);
8989
}
9090
}
91+
92+
public sealed class ErrorResilientDialogContextStore : IDialogContextStore
93+
{
94+
private readonly IDialogContextStore store;
95+
96+
public ErrorResilientDialogContextStore(IDialogContextStore store)
97+
{
98+
SetField.NotNull(out this.store, nameof(store), store);
99+
}
100+
101+
public void Save(IDialogContext context, IBotDataBag bag, string key)
102+
{
103+
this.store.Save(context, bag, key);
104+
}
105+
106+
public bool TryLoad(IBotDataBag bag, string key, out IDialogContext context)
107+
{
108+
try
109+
{
110+
return this.store.TryLoad(bag, key, out context);
111+
}
112+
catch (Exception)
113+
{
114+
// exception in loading the serialized context data
115+
context = null;
116+
return false;
117+
}
118+
}
119+
}
91120
}

0 commit comments

Comments
 (0)