I am trying to get my head around persistence and I am yet to be able to recover an actor.
My intention is to get an Actor by its persistenceId (Same way we we get an Entity using GetById in DDD).
I can get the reference to List and add it to a variable in List Manager but what I am looking for is that once the Actor dies how to get the Actor with its current state (Revovery By Events) so that modification can be done.
Let me know if my question is not clear
This is what I have done so far:
**Commands and Events **
using System;
namespace AkkaPersistence
{
public class CreateNewList
{
public string ListName { get; private set; }
public Guid UserId { get; private set; }
public string ListId { get; set; }
public CreateNewList(string listName, Guid userId, string listId)
{
ListName = listName;
UserId = userId;
ListId = listId;
}
}
public class RemoveList
{
public string ListId { get; private set; }
public Guid UserId { get; private set; }
public RemoveList(string listId, Guid userId)
{
ListId = listId;
UserId = userId;
}
}
public class ListCreated
{
public string ListName { get; private set; }
public Guid UserId { get; private set; }
public string ListId { get; private set; }
public ListCreated(string listName, Guid userId, string listId)
{
ListName = listName;
UserId = userId;
ListId = listId;
}
}
public class ListRemoved
{
public Guid UserId { get; private set; }
public string ListId { get; private set; }
public ListRemoved(Guid userId, string listId)
{
UserId = userId;
ListId = listId;
}
}
}
**List Class **
using System;
using Akka.Actor;
using Akka.Persistence;
namespace AkkaPersistence
{
public class List: ReceivePersistentActor
{
public override string PersistenceId => "AKKANETLIST";
private string Name { get; set; }
private Guid CreatedBy { get; set; }
private Guid ModifiedBy { get; set; }
public List()
{
Recover<ListCreated>(evnt =>
{
Console.WriteLine(" List :: Recovery Hit'");
Console.WriteLine("PID:{0}, Name {1}, CreatedBy:{2}, ModifiedBy{3}", PersistenceId, evnt.ListName, evnt.UserId, evnt.UserId);
Name = evnt.ListName;
CreatedBy = evnt.UserId;
ModifiedBy = evnt.UserId;
});
Command<CreateNewList>(cmd =>
{
Console.WriteLine(" List :: Received Command 'CreateNewList'");
var listCreated= new ListCreated(cmd.ListName,cmd.UserId, PersistenceId);
Persist(listCreated, lc =>
{
Console.WriteLine(" List::Event 'ListCreated' persisted");
Name = cmd.ListName;
CreatedBy = cmd.UserId;
ModifiedBy = cmd.UserId;
Sender.Tell(listCreated, ActorRefs.Nobody);
Console.WriteLine(" List::Event 'ListCreated' sent out");
});
});
Command<RemoveList>(cmd =>
{
Console.WriteLine(" List :: Received Command 'RemoveList'");
Console.WriteLine("PID:{0}, Name {1}, CreatedBy:{2}, ModifiedBy{3}",PersistenceId, Name, CreatedBy, ModifiedBy);
var listRemoved = new ListRemoved(cmd.UserId,PersistenceId);
Persist(listRemoved, lc =>
{
Console.WriteLine(" List::Event 'ListRemoved' persisted");
ModifiedBy = cmd.UserId;
Sender.Tell(listRemoved, ActorRefs.Nobody);
Console.WriteLine(" List::Event 'ListRemoved' sent out");
});
});
}
}
}
** List Manager **
using System;
using Akka.Actor;
namespace AkkaPersistence
{
public class ListManager : ReceiveActor
{
public ListManager()
{
Receive<CreateNewList>(cmd =>
{
Console.WriteLine(" List Manager:: Received Command 'CreateNewList'");
var newListRef = Context.ActorOf(Props.Create(typeof(List)));
newListRef.Tell(cmd, Self);
Console.WriteLine(" List Manager:: Command To Create New List sent to List Actor");
});
Receive<RemoveList>(cmd =>
{
Console.WriteLine(" List Manager:: Received Command 'RemoveList'");
var newListRef = Context.ActorOf(Props.Create(() => new List()), "AKKANETLIST");
newListRef.Tell(cmd, Self);
Console.WriteLine(" List Manager:: Command To 'Remove List' sent to List Actor");
});
Receive<ListCreated>(evnt =>
{
Console.WriteLine(" List Manager:: Event 'ListCreated' Received");
});
}
}
}
** Program.cs **
namespace AkkaPersistence
{
class Program
{
static void Main(string[] args)
{
var system = ActorSystem.Create("MySystem");
var listManager = system.ActorOf<ListManager>("ListManager");
// create command
var newListId = Guid.NewGuid().ToString("N");
var createCommand= new CreateNewList("Akka List 1", Guid.NewGuid(), newListId);
listManager.Tell(createCommand);
//remove Command
var removeCommand = new RemoveList(newListId, createCommand.UserId);
listManager.Tell(removeCommand);
Console.ReadLine();
}
}
}
** Console Text **
[WARNING][1/21/2017 3:11:47 PM][Thread 0009][ActorSystem(MySystem)] NewtonSoftJsonSerializer has been detected as a default serializer. It will be obsoleted in Akka.NET starting from version 1.5 in the favor of Wire (for more info visit: http://getakka.net/docs/Serialization#how-to-setup-wire-as-default-serializer ). If you want to suppress this message set HOCON `akka.suppress-json-serializer-warning` config flag to on.
List Manager:: Received Command 'CreateNewList'
List Manager:: Command To Create New List sent to List Actor
List Manager:: Received Command 'RemoveList'
List Manager:: Command To 'Remove List' sent to List Actor
List :: Received Command 'CreateNewList'
List :: Received Command 'RemoveList'
PID:AKKANETLIST, Name , CreatedBy:00000000-0000-0000-0000-000000000000, ModifiedBy00000000-0000-0000-0000-000000000000
List::Event 'ListCreated' persisted
List::Event 'ListCreated' sent out
List Manager:: Event 'ListCreated' Received
List::Event 'ListRemoved' persisted
List::Event 'ListRemoved' sent out
** Update 1 **2017-01-24
further effort, I was able to get instance of an Actor based on Name.
For this I need to create the PersistenceId as part of command and
Name the Actor with the persistence Id
By doing this I could use Context.Child(Name) to get the Actor.
I am doing a Context.Stop(newListRef) in the ListManager:Receive assuming this will stop the List Actor and force it to Recover when I access using Context.Child(Name) but that does not happen, but somehow the List State is correct. Not sure how.
I will do some more test based on comment that I received today from Horusiath
I may not quite understand your question, but when you need to recreate a persistent actor, you simply create another actor instance with the same PersistenceId.
Only thing to keep in mind here is that you should never have more than one living instance of persistent actor with the same PersistenceId at the time. Otherwise you may end with multiple actors trying to write events at the same time, possibly corrupting an event stream.
Related
I have a Payment Application in ASP.NET Core-6 Web API Entity Framework. I have this model:
Payment:
public class Payment
{
public Guid Id { get; set; }
public string ReferenceNumber { get; set; }
public string Email { get; set; }
public DateTime TransactionDate { get; set; }
public DateTime? DueDate { get; set; }
public decimal Amount { get; set; }
}
EmailSettings:
public class EmailSettings
{
public string Username { get; set; }
public string Password { get; set; }
public string DisplayName { get; set; }
public string Host { get; set; }
public int Port { get; set; }
}
EmailService:
public async Task<string> SendEmailAsync(List<string> ToEmailName, string Subject, EventModel Data)
{
_mailResponse = string.Empty;
using (SmtpClient smtpClient = new SmtpClient(_mailConfig.Host, _mailConfig.Port))
{
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new NetworkCredential(_mailConfig.Username, _mailConfig.Password);
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.SendCompleted += new SendCompletedEventHandler((object sender, AsyncCompletedEventArgs e) =>
{
_mailResponse = (e.Error != null || e.Cancelled != false) ? "failure" : "success";
});
MailMessage message = new MailMessage
{
From = new MailAddress(_mailConfig.Username, _mailConfig.DisplayName),
Subject = Subject,
SubjectEncoding = Encoding.UTF8,
BodyEncoding = Encoding.UTF8,
HeadersEncoding = Encoding.UTF8,
IsBodyHtml = true,
Body = GetEmailContent(Subject, Data),
Priority = MailPriority.High
};
foreach (string EmailName in ToEmailName)
{
message.To.Add(new MailAddress(EmailName));
}
await smtpClient.SendMailAsync(message);
}
return _mailResponse;
}
I am using HangFire.
I want the application to run a schedule using HangFire, and iterate the Payment Model using ReferenceNumber and DueDate. Then send email notification to the affected Email, 14 days to the DueDate. Reminding the affected users that his payment should be done on the DueDate
How do I achieve this?
Thanks
This can be done by scheduling notification directly based on DueDate - 14 days (or current date if that is less than 14 days away).
Also probably you would need to some extra check to your notification to see if that has already been paid before sending a reminder.
BackgroundJob.Schedule(
() => NotficationService.SendPaymentNotificationEmail(Payment.Id),
TimeSpan.FromDays(DueDate.AddDays(-14)));
Reference:
https://docs.hangfire.io/en/latest/background-methods/calling-methods-with-delay.html
I'm new to RavenDB and I'm struggling with this simple (i guess) issue.
I have a Subscriber with a collection of Subscriptions. And I want to make search by Subscription's fields, and return related Subscriber.
Here are simplified class examples:
public class Subscriber
{
public string Email { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public List<Subscription> Subscriptions { get; set; }
}
public class Subscription
{
public Guid Id { get; set; }
public string EventType { get; set; }
}
I've tried to make an index, as it is said in RavenDB docs:
public class Subscriber_BySubscription : AbstractIndexCreationTask<Subscriber>
{
public Subscriber_BySubscription()
{
Map = subscribers => from subscriber in subscribers
from subscription in subscriber.Subscriptions
select new
{
subscription.EventType,
subscription.QueueName
};
}
}
But I'm not sure that this is what I need, since query by collection using Select and Contains doesn't work. Moreover, the code looks so ugly that I feel that this is not the way how it should be.
So, I'd like to query Subscriptions by EventType, and have corresponding Subscriber as a result. In LINQ it would look like this: subscribers.Where(x => x.Subscriptions.Select(c => c.EventType).Contains(myEventType))
Managed to do it. Here is the right index:
public class Subscriber_BySubscription : AbstractIndexCreationTask<Subscriber>
{
public class Result
{
public string EventType { get; set; }
}
public Subscriber_BySubscription()
{
Map = subscribers => from subscriber in subscribers
from subscription in subscriber.Subscriptions
select new
{
subscription.EventType
};
}
}
And that's how it should be used:
var results = uow.Session
.Query<Subscriber_BySubscription.Result, Subscriber_BySubscription>()
.Where(x => x.EventType == eventType)
.OfType<Subscriber>()
.ToList();
I have a saga data class with one property marked by Unique attribute. However, this didn't prevent NServiceBus from creating several sagas with identical values in this field.
Here is my data class:
public class ModuleAliveSagaData : ContainSagaData
{
[Unique]
public string ModuleId { get; set; }
public string Endpoint { get; set; }
public string Module { get; set; }
public DateTime LastCheck { get; set; }
public bool Warning { get; set; }
public bool Error { get; set; }
}
Here is the mapping:
public override void ConfigureHowToFindSaga()
{
ConfigureMapping<ModuleAliveMessage>(m => m.Id).ToSaga(s => s.ModuleId);
}
Here is how data gets its values:
public void Handle(ModuleStartedMessage message)
{
Log.InfoFormat("Module {0} started on {1} at {2}", message.ModuleName, message.Endpoint, message.Timestamp);
Data.ModuleId = message.Id;
Data.Endpoint = message.Endpoint;
Data.Module = message.ModuleName;
Data.LastCheck = DateTime.Now;
Data.Warning = false;
Bus.SendLocal(new SendNotification
{
Subject = string.Format("Module {0} is online at {1}", Data.Module, Data.Endpoint)
});
RequestTimeout<ModuleCheckTimeout>(TimeSpan.FromMinutes(5));
Bus.Publish(new ModuleActivated
{
Endpoint = message.Endpoint,
Module = message.ModuleName
});
}
And here is what I see in the saga persistence table (Azure table storage):
Does it suppose to work like this or may be I am missing something?
Yves wrote this in comments, basically it is the proper answer:
Azure storage cannot check for uniquess besides the partitionkey/rowkey pair, so that attribute is ignored. If you need uniqueness you will have to consider another storage techology. PS: this is a known limitation of the underlying storage: http://github.com/Particular/NServiceBus.Azure/issues/21
I've got a model that represents a joint table (with payload) in my database:
public class UserHasCar
{
// Foreign keys
[Key, Column(Order = 0)]
public string ApplicationUserId { get; set; }
[Key, Column(Order = 1)]
public int CarId { get; set; }
// Navigation properties
[Required]
public virtual ApplicationUser ApplicationUser { get; set; }
[Required]
public virtual Car Car{ get; set; }
// Additional fields
public int YearsRidden { get; set; }
}
public class Car
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<UserHasCar> UserHasCars { get; set; }
}
public class ApplicationUser : IdentityUser
{
public int BirthYear{ get; set; }
public virtual ICollection<UserHasCar> UserHasCars { get; set; }
}
I have a form that includes multiple select boxes, and upon submitting I want to clear out all records related to that user who submitted the form in the UserHasCar table and replace them with the new updated information. I'm getting a An entity object cannot be referenced by multiple instances of IEntityChangeTracker. because I am doing something wrong, but I don't see where I am using more than one context. This code happens in my controller:
public ApplicationUser GetCurrentUser()
{
return UserManager.FindById(User.Identity.GetUserId());
}
public string GetUserId()
{
string id = User.Identity.GetUserId();
var user = UserManager.FindById(id);
return user.Id;
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ManageCars(FormCollection form)
{
string id = GetUserId();
// Remove cars records with my id from database
var queryCars = (from m in db.UserHasCars where m.ApplicationUserId == id select m).ToList();
foreach (var record in queryCars )
{
// Could the problem be here?
db.UserHasCars.Remove(record)
}
// Add user-submitted cars to the database
string carval = form["Cars[0]"];
Car car = (from m in db.Cars where m.Name == carval select m).First();
int carid = car.ID;
// I get the abovementioned title error here
db.UserHasCars.Add(
new UserHasCar()
{
ApplicationUser = GetCurrentUser(),
ApplicationUserId = id,
Car = car,
CarId = carid,
YearsRidden = 0
}
);
db.SaveChanges();
}
I've seen many SO posts, but can't seem the problem as why my code doesn't want to save the new database entries.
EDIT
The solution was to remove the call to get the user and replace it with a query. Why? I was making database conflict errors by having both types of calls (database and DataManager calls in the same controller action). I ended up using a modified GetUser() function instead of GetCurrentUser()
Code:
public ApplicationUser GetUser()
{
// As opposed to:
// UserManager.FindById(User.Identity.GetUserId())
// We make a database call to grab our user instead
// So we don't get database context conflicts by using UserManager
string id = GetUserId();
return db.Users.Where(m => m.Id == id).First();
}
public string GetUserId()
{
return User.Identity.GetUserId();
}
// snip
// in ManageCars(FormCollection form)
ApplicationUser user = GetUser();
// snip
var newRow = db.UserHasCars.Create();
newRow.ApplicationUser = user;
// snip
db.UserHasCars.Add(newRow);
Try removing this line:
ApplicationUser = GetCurrentUser(),
from your object instantiation when adding.
Entity populates this object automatically once you set the foreign key ApplicationUserId. If UserManager.FindById(User.Identity.GetUserId()) uses a different db context that's where your exception is coming from.
Also to save yourself further trouble down the line, you should always call db.SaveChanges() in between the two operations. If you're worried about the atomicity of the db operation, just wrap the whole thing in a Transaction.
And when adding new rows to a table, I usually prefer to use something like:
var newRow = db.SomeTable.Create();
newRow.SomeColumn1 = "something";
newRow.SomeColumn2 = 5;
db.SomeTable.Add(newRow);
db.SaveChanges();
In order to delete entries from UserHasCars you need to change their EntityState to Deleted.
Example:
foreach (var record in queryCars )
{
db.ObjectStateManager.ChangeObjectState(record, EntityState.Deleted);
}
Hope this will fix your issue.
In this website, users can register under a username and password, and can also post comments on articles. The models are pretty straightforward:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public bool IsAdmin { get; set; }
public DateTime JoinDate { get; set; }
public string AvatarPath { get; set; }
public string EmailAddress { get; set; }
}
public class ArticleComment
{
public int Id { get; set; }
public int ArticleId { get; set; }
public int UserId { get; set; }
public string CommenterName { get; set; }
public string Message { get; set; }
public DateTime CommentDate { get; set; }
public User User { get; set; }
}
Entity Framework correctly made the foreign key relationship between UserId on ArticleComment and Id on User when the database was created using code-first.
Here's my code for when a user posts a new comment:
public JsonResult SubmitComment(int articleId, string comment)
{
var response = new JsonResponse();
var currentUser = _userRepository.GetUserByUsername(User.Identity.Name);
//...
var newComment = new ArticleComment
{
ArticleId = articleId,
CommentDate = DateTime.Now,
CommenterName = currentUser.Username,
UserId = currentUser.Id,
User = currentUser,
Message = comment,
};
try
{
_articleRepository.Insert(newComment);
}
catch (Exception e)
{
response.Success = false;
response.AddError("newComment", "Sorry, we could not add your comment. Server error: " + e.Message);
return Json(response);
}
response.Success = true;
response.Value = newComment;
return Json(response);
}
The values that make up the newComment object all appear to be correct, and the Insert method in my Article repository class is straight and to the point:
public void Insert(ArticleComment input)
{
DataContext.ArticleComments.Add(input);
DataContext.SaveChanges();
}
But once this happens, poof: a new record in my Users table appears along with the new record in ArticleComments. All of the info in the new Users record is duplicated from that user's existing record - the only difference is the value for the primary key Id. What gives?
In addition to my comment, you need to make sure that both _userRepository and _articleRepository are using the same DbContext instance.
Either that, or you can try this:
var newComment = new ArticleComment
{
ArticleId = articleId,
CommentDate = DateTime.Now,
CommenterName = currentUser.Username,
UserId = currentUser.Id,
// User = currentUser, let the UserId figure out the User, don't set it yourself.
Message = comment,
};