protected override void Load()

in Azure/CSharp/Library/Microsoft.Bot.Builder.Azure/AzureModule.cs [79:184]


        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<ConnectorStore>()
                .AsSelf()
                .InstancePerLifetimeScope();

            // if application settings indicate that bot should use the table storage, 
            // TableBotDataStore will be registered as underlying storage
            // otherwise bot connector state service will be used.
            if (ShouldUseTableStorage())
            {
                builder.Register(c => MakeTableBotDataStore())
                    .Keyed<IBotDataStore<BotData>>(Key_DataStore)
                    .AsSelf()
                    .SingleInstance();
            }
            else if (ShouldUseTableStorage2())
            {
                builder.Register(c => MakeTableBotDataStore2())
                    .Keyed<IBotDataStore<BotData>>(Key_DataStore)
                    .AsSelf()
                    .SingleInstance();
            }
            else if (ShouldUseCosmosDb())
            {
                builder.Register(c => MakeCosmosDbBotDataStore())
                    .Keyed<IBotDataStore<BotData>>(Key_DataStore)
                    .AsSelf()
                    .SingleInstance();
            }
            else if (ShouldUseSqlServer())
            {
                SqlBotDataContext.AssertDatabaseReady();

                builder.Register(c => MakeSqlBotDataStore())
                    .Keyed<IBotDataStore<BotData>>(Key_DataStore)
                    .AsSelf()
                    .SingleInstance();
            }
            else
            {
                builder.Register(c => new ConnectorStore(c.Resolve<IStateClient>()))
                    .Keyed<IBotDataStore<BotData>>(Key_DataStore)
                    .AsSelf()
                    .InstancePerLifetimeScope();
            }

            // register the data store with caching data store
            // and set the consistency policy to be "Last write wins".
            builder.Register(c => new CachingBotDataStore(c.ResolveKeyed<IBotDataStore<BotData>>(Key_DataStore),
                        CachingBotDataStoreConsistencyPolicy.LastWriteWins))
                    .As<IBotDataStore<BotData>>()
                    .AsSelf()
                    .InstancePerLifetimeScope();

            // register the appropriate StateClient based on the state api url.
            builder.Register(c =>
                {
                    var activity = c.Resolve<IActivity>();
                    if (activity.ChannelId == "emulator")
                    {
                        // for emulator we should use serviceUri of the emulator for storage
                        return new StateClient(new Uri(activity.ServiceUrl));
                    }

                    MicrosoftAppCredentials.TrustServiceUrl(BotService.stateApi.Value, DateTime.MaxValue);
                    return new StateClient(new Uri(BotService.stateApi.Value));
                })
            .As<IStateClient>()
            .InstancePerLifetimeScope();
            
            // register the bot service serialization binder for type mapping to current assembly
            builder.Register(c => new BotServiceSerializationBinder(assembly))
                .AsSelf()
                .As<SerializationBinder>()
                .InstancePerLifetimeScope();

            // register the Delegate surrogate provide to map delegate to current assembly during deserialization
            builder
                .Register(c => new BotServiceDelegateSurrogate(assembly))
                .AsSelf()
                .InstancePerLifetimeScope();

            // extend surrogate providers with bot service delegate surrogate provider and register the surrogate selector
            builder
                .Register(c =>
                    {
                        var providers = c.ResolveKeyed<IEnumerable<Serialization.ISurrogateProvider>>(FiberModule.Key_SurrogateProvider).ToList();
                        // need to add the latest delegate surrogate to make sure that surrogate selector
                        // can deal with latest assembly
                        providers.Add(c.Resolve<BotServiceDelegateSurrogate>());
                        return new Serialization.SurrogateSelector(providers);
                    })
                .As<ISurrogateSelector>()
                .InstancePerLifetimeScope();

            // register binary formatter used for binary serialization operation
            builder
                .Register((c, p) => new BinaryFormatter(c.Resolve<ISurrogateSelector>(), new StreamingContext(StreamingContextStates.All, c.Resolve<IResolver>(p)))
                {
                    AssemblyFormat = FormatterAssemblyStyle.Simple,
                    Binder = c.Resolve<SerializationBinder>()
                })
                .As<IFormatter>()
                .InstancePerLifetimeScope();
        }