[Question] Handle 412 Issue with Table Bot Data Store
System Information
- SDK Language: .NET/C#
- SDK Version: Bot Builder 4.0.30319
- Development Environment: Azure App Service
Issue Description
Recently, we shifted our bot data store to use Azure Table. The following piece of code is used to handle precondition failed ( 412 ) error but this does use c.Resolve for connector store however, I am looking forward to use TableStore. So let me know how to access the Table Bot Data Store using AutoFac container.
var builder = new ContainerBuilder();
builder
.Register(c => new CachingBotDataStore(c.Resolve
Code Example
I believe the solution which I am looking for is related to the AutoFac settings. I have instantiated a table bot data store in Global.asax,
var builder = new ContainerBuilder();
var store = new TableBotDataStore(Configuration.StorageConnectionString);
builder.Register(c => store)
.Keyed<IBotDataStore
However, in a different class(ConversationWithMultiCredentials.cs) before each sendAsync I have to set to the caching store consistency policy to avoid 412 issues,
// See https://docs.microsoft.com/en-us/bot-framework/troubleshoot-general-problems#how-can-i-fix-precondition-failed-412-or-conflict-409-http-errors
builder
.Register(c => new CachingBotDataStore(c.ResolveKeyed<IBotDataStore
This ConnectorStore should actually be replaced with the TableBotDataStore. So, please let me know how to use the table bot data store instead of the connector store.
Can you please share the code?
Thanks for sharing code with me., I will try to repro the issue.
@raghavan
Here is the modified class ConversationWithCredentials
and here is the previous version
- Updated with Azure TableBotDataStore using Autofac implementation.
- Introduced static constructor and static Container object in order to isolate instantiation from SendAsync calls.
Hope, this helps.
Hi msmohan , Can you please share the latest code.We are facing the same issue. I am not able to access the code from the link that you provided.
@pkpaul5 here you go.
using Autofac; using Microsoft.Bot.Builder.Azure; using Microsoft.Bot.Builder.Base; using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Builder.Dialogs.Internals; using Microsoft.Bot.Connector; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web; using Microsoft.WindowsAzure.Storage; using System.Reflection;
namespace LocalBot.Helpers { public static class ConversationWithCredentials { private static readonly object locker = new object(); private static IContainer container;
public static IContainer Container
{
get
{
lock (locker)
{
return container;
}
}
}
static ConversationWithCredentials()
{
UpdateContainer(ContainerBuilder => { });
}
public static void UpdateContainer(Action<ContainerBuilder> update)
{
lock (locker)
{
var builder = new ContainerBuilder();
builder.RegisterModule(new DialogModule_MakeRoot());
builder.RegisterModule(new AzureModule(Assembly.GetExecutingAssembly()));
var store = new TableBotDataStore(CloudStorageAccount.DevelopmentStorageAccount);
builder.Register(c => store)
.Keyed<IBotDataStore<BotData>>(AzureModule.Key_DataStore)
.AsSelf()
.SingleInstance();
//LastWriteWins
builder
.Register(c => new CachingBotDataStore(c.ResolveKeyed<IBotDataStore<BotData>>(typeof(ConnectorStore)), CachingBotDataStoreConsistencyPolicy.LastWriteWins))
.As<IBotDataStore<BotData>>()
.AsSelf()
.InstancePerLifetimeScope();
builder
.Register(c => new MicrosoftAppCredentials("msaAppId", "msaPassword")) //replace this!
.As<MicrosoftAppCredentials>()
.InstancePerLifetimeScope();
update(builder);
container = builder.Build();
}
}
public static async Task SendAsync(IMessageActivity toBot, Func<IDialog<object>> makeRoot, CancellationToken token = default(CancellationToken))
{
Trace.TraceInformation(TraceHelper.AppendMetaData(". Attempting to route bot...", toBot));
using (var scope = DialogModule.BeginLifetimeScope(container, toBot))
{
DialogModule_MakeRoot.Register(scope, makeRoot);
await SendAsync(scope, toBot, token);
}
}
private static async Task SendAsync(ILifetimeScope scope, IMessageActivity toBot, CancellationToken token = default(CancellationToken))
{
using (new LocalizedScope(toBot.Locale))
{
var task = scope.Resolve<IPostToBot>();
await task.PostAsync(toBot, token);
}
}
}
}