How does Akka.NET persistence handle replaying messages containing IActorRef? - c#

If I send an Akka.NET actor a message which is an object containing an IActorRef, and then persist that message, the JSON written to the journal table looks like this:
{"$id":"1","$type":"LearningAkka.Program+BindReference, LearningAkka","Reference":{"$id":"2","$type":"Akka.Actor.ActorRefBase+Surrogate, Akka","Path":"akka://LearningAkka/user/$b#1222898859"}}
If I'm understanding this right, this is just a reference to an actor instance; the "Props" required to create it are not stored in this message.
Weirdly, I am seeing an object there after restarting the app. However, as expected, it is not as constructed before the restart. Where did this actor come from? Has Akka Persistence found an actor which is "similar enough" and used it instead?
The following C# test application creates an object and sends a message binding it to one of three others. After disposing of the actor system, that object is recreated from persistence (SQL Server) and the reference is checked.
My expected behaviour is any of the following (I'm not sure what's most appropriate):
The actor can't be created because one of its messages contains an unresolvable reference.
The actor reference is null because it cannot be resolved.
The actor reference points to dead letters or similar.
Console output:
[WARNING][27/05/2017 21:02:27][Thread 0001][ActorSystem(LearningAkka)] NewtonSoftJsonSerializer has been detected as a default serializer. It will be obsoleted in Akka.NET starting from version 1.5 in the favor of Hyperion (for more info visit: http://getakka.net/docs/Serialization#how-to-setup-hyperion-as-default-serializer ). If you want to suppress this message set HOCON `akka.suppress-json-serializer-warning` config flag to on.
From the first run B
[WARNING][27/05/2017 21:02:28][Thread 0001][ActorSystem(LearningAkka)] NewtonSoftJsonSerializer has been detected as a default serializer. It will be obsoleted in Akka.NET starting from version 1.5 in the favor of Hyperion (for more info visit: http://getakka.net/docs/Serialization#how-to-setup-hyperion-as-default-serializer ). If you want to suppress this message set HOCON `akka.suppress-json-serializer-warning` config flag to on.
From the second run B
C#:
using Akka.Actor;
using Akka.Event;
using Akka.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearningAkka
{
class Program
{
static void Main(string[] args)
{
using (var actorSystem = ActorSystem.Create("LearningAkka"))
{
var referenceA = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the first run A")));
var referenceB = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the first run B")));
var referenceC = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the first run C")));
var actor = actorSystem.ActorOf(Props.Create(() => new TestActor()));
actor.Tell(new BindReference { Reference = referenceB });
actor.Tell(new CheckReference());
Console.ReadLine();
}
using (var actorSystem = ActorSystem.Create("LearningAkka"))
{
var referenceA = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the second run A")));
var referenceB = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the second run B")));
var referenceC = actorSystem.ActorOf(Props.Create(() => new TestReferencedActor("From the second run C")));
var actor = actorSystem.ActorOf(Props.Create(() => new TestActor()));
actor.Tell(new CheckReference());
Console.ReadLine();
}
}
public struct BindReference { public IActorRef Reference; }
public struct CheckReference { }
public sealed class TestActor : ReceivePersistentActor
{
public override string PersistenceId => "test hardcoded";
private IActorRef StoredFromMessage;
public TestActor()
{
Command<CheckReference>(m => StoredFromMessage.Tell(m));
Command<BindReference>(m => Persist(m, m2 => StoredFromMessage = m2.Reference));
Recover<BindReference>(m => StoredFromMessage = m.Reference);
}
}
public sealed class TestReferencedActor : ReceiveActor
{
public TestReferencedActor(string ourLabel)
{
Receive<CheckReference>(m => Console.WriteLine(ourLabel));
}
}
}
}
HOCON:
akka {
persistence {
journal {
plugin = "akka.persistence.journal.sql-server"
sql-server {
class = "Akka.Persistence.SqlServer.Journal.SqlServerJournal, Akka.Persistence.SqlServer"
connection-string = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=LearningAkka;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
schema-name = dbo
table-name = Journal
auto-initialize = on
}
}
snapshot-store {
plugin = "akka.persistence.snapshot-store.sql-server"
sql-server {
class = "Akka.Persistence.SqlServer.Snapshot.SqlServerSnapshotStore, Akka.Persistence.SqlServer"
connection-string = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=LearningAkka;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
schema-name = dbo
table-name = Snapshot
auto-initialize = on
}
}
}
}
Could someone please comment on the behaviour here? Thank you.

As you can see from serialization data - your IActorRef points to this address akka://LearningAkka/user/$b. Where $b is usually placed for unnamed actors. So it will always be the second unnamed actor you create in the actor system root (as far as I know).
So you are right - the system behavior is undefined here.

Related

How can we log both user and bot messages to comos db in microsoft bot framework

I have created a chat bot using microsoft bot framework v4 sdk. I wanted to log bot user and bot messages to cosmos db.
i am able to log only user messages using below blog https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-howto-v4-storage?view=azure-bot-service-4.0&tabs=csharp#using-cosmos-db .
I expect to log both user and bot responses.
Thankfully, this is pretty easy since ItranscriptLogger and TranscriptLoggerMiddleware already exist.
Create your TranscriptStore Class (new Class file)
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Azure;
using Microsoft.Bot.Schema;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace QuickTestBot_CSharp
{
public class CosmosTranscriptStore : ITranscriptLogger
{
private CosmosDbStorage _storage;
public CosmosTranscriptStore(CosmosDbStorageOptions config)
{
_storage = new CosmosDbStorage(config);
}
public async Task LogActivityAsync(IActivity activity)
{
// activity only contains Text if this is a message
var isMessage = activity.AsMessageActivity() != null ? true : false;
if (isMessage)
{
// Customize this to save whatever data you want
var data = new
{
From = activity.From,
To = activity.Recipient,
Text = activity.AsMessageActivity().Text,
};
var document = new Dictionary<string, object>();
// activity.Id is being used as the Cosmos Document Id
document.Add(activity.Id, data);
await _storage.WriteAsync(document, new CancellationToken());
}
}
}
}
Create and Add the Middleware (in Startup.cs)
[...]
var config = new CosmosDbStorageOptions
{
AuthKey = "<YourAuthKey>",
CollectionId = "<whateverYouWant>",
CosmosDBEndpoint = new Uri("https://<YourEndpoint>.documents.azure.com:443"),
DatabaseId = "<whateverYouWant>",
};
var transcriptMiddleware = new TranscriptLoggerMiddleware(new CosmosTranscriptStore(config));
var middleware = options.Middleware;
middleware.Add(transcriptMiddleware);
[...]
Result:
Note:
This is probably the easiest/best way to do it. However, you can also capture outgoing activities under OnTurnAsync() using turnContext.OnSendActivities() and then write the outgoing activity to storage, as well.

Retrieve TFS Test Steps always returns count of 0

I am trying to retrieve test steps (aka "actions") that have been added to a test case in TFS (2017.2) using the API (Microsoft.TeamFoundationServer.ExtendedClient v15.112.1). My current implementation always returns 0 test steps, although the actual test case has steps. I tried this as well in a clean new Team Project without any Work Item customization and even there it returns 0 steps. My implementation uses the older API (based on SOAP webservices), because it seems the newer http based API does not yet implement test steps. This is the code I have used:
private void GetTestStepsForTestCase(int testCaseId, int testSuiteId,
string teamProjectName, Uri tfsUrl)
{
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(tfsUrl);
ITestManagementService itms = tpc.GetService<ITestManagementService>();
ITestManagementTeamProject ittp = itms.GetTeamProject(teamProjectName);
ITestSuiteBase suite = ittp.TestSuites.Find(testSuiteId);
ITestCaseCollection testCaseCollection = suite.AllTestCases;
ITestCase itestCase = testCaseCollection.FirstOrDefault(t => t.Id == testCaseId);
foreach (Microsoft.TeamFoundation.TestManagement.Client.ITestAction itestAction in itestCase.Actions)
{
// Do something
}
}
Anyone?
You can use below sample to retrieve the test case steps from a specific test suite, it works on my side:
Install the nuget package : Microsoft.TeamFoundationServer.ExtendedClient - 15.112.1
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.VisualStudio.Services.Client;
using System;
namespace RetrieveTestSteps
{
class Program
{
static void Main(string[] args)
{
var u = new Uri("http://server:8080/tfs/DefaultCollection");
var c = new VssClientCredentials();
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(u, c);
tpc.EnsureAuthenticated();
ITestManagementService itms = tpc.GetService<ITestManagementService>();
ITestManagementTeamProject ittp = itms.GetTeamProject("LCScrum");
ITestSuiteBase suite = ittp.TestSuites.Find(352);
ITestCaseCollection testCaseCollection = suite.AllTestCases;
foreach (var tc in testCaseCollection)
{
ITestCase testcase = ittp.TestCases.Find(tc.Id);
foreach (ITestAction action in testcase.Actions)
{
Console.WriteLine(String.Format("{0} - {1}", testcase.Id, action));
}
}
Console.Read();
}
}
}
OK, I finally figured out this one myself. The answer and comments of Andy helped me validate that my code is correct. I just discovered that my code worked fine when NOT debugging! When debugging, at some point I noticed this:
So probably because of lazy loading somewhere, it is not possible to verify the count of the attachments debug-time (see post here: Lazy<T>: "The function evaluation requires all threads to run").

c# Cannot create InlineKeyboardButton

I am creating a telegram bot, but I am unable to create any InlineKeyboardButton objects.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telegram.Bot.Types.InlineKeyboardButtons;
namespace BuildAutomation.Controllers
{
public class Test
{
public TestMethod()
{
var none = new InlineKeyboardButton("No", "build|no");
var yes = "Yes";
var betaControl = new InlineKeyboardButton(yes, "build|betacontrol");
var betaNode = new InlineKeyboardButton(yes, "build|betanode");
var betaBoth = new InlineKeyboardButton(yes, "build|betaboth");
InlineKeyboardMarkup menu;
menu = new InlineKeyboardMarkup(new[] { betaBoth, none });
}
}
}
I keep getting the error 'Cannot create an instance of the abstract class or interface 'InlineKeyboardButton'. I realize that InlineKeyboardButton is an abstract class, but i see many examples creating an object of InlineKeyboardButton.
Did I miss something?
example 1
example 2
example 3
Instantiation of InlineKeyboardButton directly was correct in previous versions.
There is a new commit for about one month ago which indicates that InlineKeyboardButton is made abstract from then on. You must use derived classes instead. InlineKeyboardUrlButton, InlineKeyboardPayButton and etc. are all derived from InlineKeyboardButton.
It seems that the examples' repository is not updated yet.
Check this link for more details about the mentioned commit:
https://github.com/TelegramBots/telegram.bot/commit/ddaa8b74e3ab5eab632dbe2e8916c2fe87b114a3
Use InlineKeyboardCallbackButton Example, InlineKeyboardCallbackButton("Yes", "CallBackData") instead of InlineKeyboardButton("Yes", "CallbackData")
You couldn't create a new instance of abstraction class. So use sample bellow
var KeyboardButons = new InlineKeyboardButton[][]
{
new InlineKeyboardButton[]
{
InlineKeyboardButton.WithCallbackData("سفارش", callbackQueryData) ,
InlineKeyboardButton.WithCallbackData("بازگشت", "return")
}
};
var replyMarkup = new InlineKeyboardMarkup()
{
InlineKeyboard = KeyboardButons
};

C# Windows Service - this keyword error

I have the following code which works fine when I use it within a Windows Forms application, however the application I'm writing needs to run as a Windows service, and when I moved my code into the Windows Service template in Visual Studio 2015 Community Edition, I get the following error.
Cannot implicitly convert type "MyWindowsService.Main" to "System.ComponentModel.ISynchronizeVoke". An explicit conversion exists (are you missing a cast?)
Could anyone shed some light on why I am getting this error, and what I need to do to resolve it?
The code which throws the error is the line below, and it is located within the OnStart method of my main class (named Main.cs). The code is used to create an instance of the DataSubscriber class (AdvancedHMI library).
dataSubscribers[dataSubscriberIndex].SynchronizingObject = this;
It has to have something to do with the fact that the code is in a Windows service template, because using this works perfectly in my forms application running the same code.
UPDATE
Correction, I've attempted to cast this to the required type, and now get the following error on run.
Additional information: Unable to cast object of type 'MyWindowsService.Main' to type 'System.ComponentModel.ISynchronizeInvoke'.
Code:
dataSubscribers[dataSubscriberIndex].SynchronizingObject = (System.ComponentModel.ISynchronizeInvoke)this;
UPDATE
I've included the entire contents of the Main.cs file from my Windows Service application.
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using AdvancedHMIDrivers;
using AdvancedHMIControls;
using MfgControl.AdvancedHMI.Drivers;
using MfgControl.AdvancedHMI.Controls;
using System.Collections.ObjectModel;
namespace PLCHistoricDataHarvester {
public partial class Main : ServiceBase {
private EthernetIPforCLXCom commObject = new EthernetIPforCLXCom();
private globals globals = new globals();
private Dictionary<String, String> operationLines = new Dictionary<String, String>();
private Dictionary<String, String> tags = new Dictionary<String, String>();
private Collection<DataSubscriber> dataSubscribers = new Collection<DataSubscriber>();
private int harvesterQueueCount = 0;
private string harvesterInsertValues = String.Empty;
public Main() {
InitializeComponent();
}
protected override void OnStart(string[] args) {
// Initialize our harvester program
initializeHarvester();
Console.WriteLine("The program has started");
}
protected override void OnStop() {
// Call code when the service is stopped
Console.WriteLine("Program has stopped");
Console.ReadLine();
}
public void initializeHarvester() {
// First, we connect to the database using our global connection object
globals.dbConn.DatabaseName = "operations";
if (!globals.dbConn.IsConnect()) {
// TODO: Unable to connect to database. What do we do?
}
// Second, we connect to the database and pull data from the settings table
globals.initializeSettingsMain();
// Set IP address of PLC
commObject.IPAddress = globals.getSettingsMain("Processor_IP");
// Pull distinct count of our parent tags (Machines ex: Line 1, etc)
operationLines = globals.getOperationLines();
// If we have at least 1 operation line defined...we continue
if (operationLines.Keys.Count > 0) {
//Now we loop over the operation lines, and pull back the data points
int dataSubscriberIndex = 0;
foreach (KeyValuePair<String, String> lines in operationLines) {
int line_id = int.Parse(lines.Key);
string name = lines.Value;
tags = globals.getTags(line_id);
// If we have at least 1 tag for this operation line, we continue...
if (tags.Keys.Count > 0 && tags["tags"].ToString().IndexOf(",") != -1) {
// Create our dataSubscriber object
dataSubscribers.Add(new DataSubscriber());
dataSubscribers[dataSubscriberIndex].SynchronizingObject = (ISynchronizeInvoke)this;
dataSubscribers[dataSubscriberIndex].CommComponent = commObject;
dataSubscribers[dataSubscriberIndex].PollRate = 1000;
dataSubscribers[dataSubscriberIndex].PLCAddressValue = tags["tags"];
dataSubscribers[dataSubscriberIndex].DataChanged += new EventHandler<MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs>(subscribeCallback);
// Increment our dataSubscriberIndex
dataSubscriberIndex++;
}
}
}
}
private void subscribeCallback(object sender, MfgControl.AdvancedHMI.Drivers.Common.PlcComEventArgs e) {
// code removed as it is irrelevant
}
}
}
The error message says this:
An explicit conversion exists (are you missing a cast?)
So add a cast like this:
dataSubscribers[dataSubscriberIndex].SynchronizingObject = (ISynchronizeInvoke)this;
^^^^^^^^^^^^^^^^^^^^
//Add this
If you've got a console app, the easiest way to convert it to a windows service is by using Topshelf, a nuget package which lets you run in either console mode or nt service mode.
Here's the quickstart guide.
We use it to write services all the time and it helps you avoid this kind of fragile shenanigans.

How can I roll back changes with Entity Framework 5 within a Unit Test project

I'm playing with Entity Framework, and I have a Unit Test project that I want to exercise what I've done so far. I'd like to have it not actually update my test database when it's done. If I was working in SQL I would create a transaction and then roll it back at the end.
How can I do the same thing here?
As I understand it, context.SaveChanges(); is effectively doing the write to the database. And if I don't have that, then allCartTypes is empty after I assign it context.CarTypes.ToList()
Here's an example of one of my Test classes.
using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Trains;
using System.Linq;
namespace TrainsTest
{
[TestClass]
public class TestCarType : TestBase
{
[TestMethod]
public void TestCarTypeCreate_Success()
{
var tankerCarType = new CarType {Name = "Tanker"};
var boxCarType = new CarType { Name = "Box" };
using (var context = new TrainEntities())
{
context.CarTypes.Add(tankerCarType);
context.CarTypes.Add(boxCarType);
context.SaveChanges();
var allCartTypes = context.CarTypes.ToList();
foreach (var cartType in allCartTypes)
{
Debug.WriteLine(cartType.CarTypeId + " - " + cartType.Name);
}
}
}
}
}
I know I'm missing something fundamental, but I don't know what it is. and my googling has been fruitless.
There's a MSDN article about ef transactions.
http://msdn.microsoft.com/en-gb/library/vstudio/bb738523(v=vs.100).aspx

Categories

Resources