Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3027580

Browse files
author
Catalin Hatmanu
committed
Upgrade to LiteDb 5 and BotFramework 4.9.3
1 parent 4aca415 commit 3027580

File tree

9 files changed

+94
-78
lines changed

9 files changed

+94
-78
lines changed

‎BinaryFog.Bot.Builder.LiteDb.Tests/BinaryFog.Bot.Builder.LiteDb.Tests.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
11-
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
12-
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
1313
</ItemGroup>
1414

1515
<ItemGroup>
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<Company>BinaryFog.com</Company>
77
<Authors>Catalin Hatmanu</Authors>
8-
<Copyright>BinaryFog.com 2019</Copyright>
9-
<Version>1.0.0</Version>
8+
<Copyright>BinaryFog.com 2020</Copyright>
9+
<Version>2.0.0</Version>
1010
<Description>Microsoft Bot Builder Storage provider based on LiteDb database</Description>
11-
<PackageReleaseNotes>Upgraded to Bot Builder 4.4.5</PackageReleaseNotes>
11+
<PackageReleaseNotes>Upgraded to Bot Builder 4.9.3 and LiteDb 5.0.8</PackageReleaseNotes>
1212
<RepositoryUrl>https://github.com/binaryfog/BinaryFog.Bot.Builder.LiteDb</RepositoryUrl>
1313
<PackageProjectUrl>https://github.com/binaryfog/BinaryFog.Bot.Builder.LiteDb</PackageProjectUrl>
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="LiteDB" Version="4.1.4" />
18-
<PackageReference Include="Microsoft.Bot.Builder" Version="4.4.5" />
17+
<PackageReference Include="LiteDB" Version="5.0.8" />
18+
<PackageReference Include="Microsoft.Bot.Builder" Version="4.9.3" />
1919
</ItemGroup>
2020

2121
</Project>

‎BinaryFog.Bot.Builder.LiteDb/LiteDbStorage.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LiteDB;
2+
using LiteDB.Engine;
23
using Microsoft.Bot.Builder;
34
using Newtonsoft.Json;
45
using Newtonsoft.Json.Linq;
@@ -55,12 +56,15 @@ public LiteDbStorage()
5556
throw new ArgumentNullException(nameof(keys));
5657
}
5758

58-
using (var db = new LiteEngine(databaseFileName))
59+
using (var db = new LiteDatabase(databaseFileName))
5960
{
61+
var col = db.GetCollection(CollectionName);
62+
6063
foreach (var key in keys)
6164
{
6265
//_memory.Remove(key);
63-
db.Delete(CollectionName, Query.EQ(KeyName, key));
66+
//db.Delete(CollectionName, Query.EQ(KeyName, key));
67+
col.Delete(key);
6468
}
6569
}
6670

@@ -87,11 +91,15 @@ public LiteDbStorage()
8791

8892

8993
var storeItems = new Dictionary<string, object>(keys.Length);
90-
using (var db = new LiteEngine(databaseFileName))
94+
using (var db = new LiteDatabase(databaseFileName))
9195
{
96+
var col = db.GetCollection(CollectionName);
97+
9298
foreach (var key in keys)
9399
{
94-
var value = db.Find(CollectionName, Query.EQ(KeyName, key)).FirstOrDefault();
100+
//var value = db.Find(CollectionName, Query.EQ(KeyName, key)).FirstOrDefault();
101+
var value = col.Find( Query.EQ(KeyName, key) ).FirstOrDefault();
102+
95103
if (value != null)
96104
{
97105
var state = JsonConvert.DeserializeObject(value[ContentName], settings);
@@ -124,16 +132,18 @@ public LiteDbStorage()
124132
throw new ArgumentNullException(nameof(changes));
125133
}
126134

127-
using (var db = new LiteEngine(databaseFileName))
135+
using (var db = new LiteDatabase(databaseFileName))
128136
{
129137

130-
foreach (var change in changes)
138+
var col = db.GetCollection(CollectionName);
139+
foreach (var change in changes)
131140
{
132141
var newValue = change.Value;
133142

134143
var json = JsonConvert.SerializeObject(newValue, typeof(object), settings);
135144

136-
db.Upsert(CollectionName, new BsonDocument { { KeyName, change.Key }, { ContentName, json } }); // false (update)
145+
//db.Upsert(CollectionName, new BsonDocument { { KeyName, change.Key }, { ContentName, json } }); // false (update)
146+
col.Upsert(new BsonDocument { { KeyName, change.Key }, { ContentName, json } });
137147

138148
}//end using
139149
}

‎StateManagementBot/AdapterWithErrorHandler.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
5+
using Microsoft.Bot.Builder;
46
using Microsoft.Bot.Builder.Integration.AspNet.Core;
5-
using Microsoft.Bot.Connector.Authentication;
7+
using Microsoft.Bot.Builder.TraceExtensions;
8+
using Microsoft.Extensions.Configuration;
69
using Microsoft.Extensions.Logging;
710

811
namespace Microsoft.BotBuilderSamples
912
{
1013
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
1114
{
12-
public AdapterWithErrorHandler(ICredentialProvidercredentialProvider, ILogger<BotFrameworkHttpAdapter> logger)
13-
: base(credentialProvider)
15+
public AdapterWithErrorHandler(IConfigurationconfiguration, ILogger<BotFrameworkHttpAdapter> logger,ConversationStateconversationState=null)
16+
: base(configuration,logger)
1417
{
15-
// Enable logging at the adapter level using OnTurnError.
1618
OnTurnError = async (turnContext, exception) =>
1719
{
18-
logger.LogError($"Exception caught : {exception}");
19-
await turnContext.SendActivityAsync("Sorry, it looks like something went wrong.");
20+
// Log any leaked exception from the application.
21+
// NOTE: In production environment, you should consider logging this to
22+
// Azure Application Insights. Visit https://aka.ms/bottelemetry to see how
23+
// to add telemetry capture to your bot.
24+
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
2025

21-
// Depending on the application and how the state is being used you may also decide to
22-
// delete either the ConversationState or UserState or both when you get a leaked global exception.
23-
// This can avoid a bot getting "stuck" in an infinite loop prompting the user.
26+
// Send a message to the user
27+
await turnContext.SendActivityAsync("The bot encountered an error or bug.");
28+
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
29+
30+
if (conversationState != null)
31+
{
32+
try
33+
{
34+
// Delete the conversationState for the current conversation to prevent the
35+
// bot from getting stuck in a error-loop caused by being in a bad state.
36+
// ConversationState should be thought of as similar to "cookie-state" in a Web pages.
37+
await conversationState.DeleteAsync(turnContext);
38+
}
39+
catch (Exception e)
40+
{
41+
logger.LogError(e, $"Exception caught on attempting to Delete ConversationState : {e.Message}");
42+
}
43+
}
44+
45+
// Send a trace activity, which will be displayed in the Bot Framework Emulator
46+
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
2447
};
2548
}
2649
}

‎StateManagementBot/ConfigurationCredentialProvider.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎StateManagementBot/Program.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using Microsoft.AspNetCore;
54
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
66
using Microsoft.Extensions.Logging;
77

88
namespace Microsoft.BotBuilderSamples
@@ -11,16 +11,19 @@ public class Program
1111
{
1212
public static void Main(string[] args)
1313
{
14-
CreateWebHostBuilder(args).Build().Run();
14+
CreateHostBuilder(args).Build().Run();
1515
}
1616

17-
public static IWebHostBuilderCreateWebHostBuilder(string[] args) =>
18-
WebHost.CreateDefaultBuilder(args)
19-
.ConfigureLogging((logging) =>
17+
public static IHostBuilderCreateHostBuilder(string[] args) =>
18+
Host.CreateDefaultBuilder(args)
19+
.ConfigureWebHostDefaults(webBuilder =>
2020
{
21-
logging.AddDebug();
22-
logging.AddConsole();
23-
})
24-
.UseStartup<Startup>();
21+
webBuilder.ConfigureLogging((logging) =>
22+
{
23+
logging.AddDebug();
24+
logging.AddConsole();
25+
});
26+
webBuilder.UseStartup<Startup>();
27+
});
2528
}
2629
}

‎StateManagementBot/Startup.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Hosting;
6-
using Microsoft.AspNetCore.Mvc;
76
using Microsoft.Bot.Builder;
87
using Microsoft.Bot.Builder.Integration.AspNet.Core;
9-
using Microsoft.Bot.Connector.Authentication;
8+
using Microsoft.Extensions.Hosting;
109
using Microsoft.Extensions.DependencyInjection;
1110

1211
namespace Microsoft.BotBuilderSamples
@@ -16,44 +15,42 @@ public class Startup
1615
// This method gets called by the runtime. Use this method to add services to the container.
1716
public void ConfigureServices(IServiceCollection services)
1817
{
19-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
20-
21-
// Create the credential provider to be used with the Bot Framework Adapter.
22-
services.AddSingleton<ICredentialProvider, ConfigurationCredentialProvider>();
18+
services.AddControllers().AddNewtonsoftJson();
2319

24-
// Create the Bot Framework Adapter with error handling enabled.
20+
// Create the Bot Framework Adapter with error handling enabled.
2521
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
2622

2723
// Create the storage we'll be using for User and Conversation state. (Memory is great for testing purposes.)
28-
services.AddSingleton<IStorage,BinaryFog.Bot.Builder.LiteDb.LiteDbStorage>();
24+
varstorage=newBinaryFog.Bot.Builder.LiteDb.LiteDbStorage();
2925

30-
// Create the User state.
31-
services.AddSingleton<UserState>();
26+
// Create the User state passing in the storage layer.
27+
var userState = new UserState(storage);
28+
services.AddSingleton(userState);
29+
30+
// Create the Conversation state passing in the storage layer.
31+
var conversationState = new ConversationState(storage);
32+
services.AddSingleton(conversationState);
3233

33-
// Create the Conversation state.
34-
services.AddSingleton<ConversationState>();
35-
3634
// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
3735
services.AddTransient<IBot, StateManagementBot>();
3836
}
3937

4038
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
41-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
39+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4240
{
4341
if (env.IsDevelopment())
4442
{
4543
app.UseDeveloperExceptionPage();
4644
}
47-
else
48-
{
49-
app.UseHsts();
50-
}
51-
52-
app.UseDefaultFiles();
53-
app.UseStaticFiles();
5445

55-
//app.UseHttpsRedirection();
56-
app.UseMvc();
46+
app.UseDefaultFiles()
47+
.UseStaticFiles()
48+
.UseRouting()
49+
.UseAuthorization()
50+
.UseEndpoints(endpoints =>
51+
{
52+
endpoints.MapControllers();
53+
});
5754
}
5855
}
5956
}

‎StateManagementBot/StateMangementBot.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.2</TargetFramework>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
55
</PropertyGroup>
66

77
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
@@ -13,9 +13,8 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="Microsoft.AspNetCore.App" />
17-
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
18-
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.4.5" />
16+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
17+
<PackageReference Include="Microsoft.Bot.Builder.Integration.AspNet.Core" Version="4.9.3" />
1918
</ItemGroup>
2019

2120
<ItemGroup>

‎StateManagementBot/mioData.db

12 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /