dotnet-core, angular, 5.4.1 - One for Aaron. I think. Hi Aaron, If you remember I implemented my own EntityHistoryStore inheriting from IEntityHistoryStore to write entity change history to Mongodb. This works well but the other day Azure reconfigured my outgoing Ip's for some reason and the new IP's were not configured in my Mongodb Atlas whitelist. I fixed this issue but realised that I would need a fallback should, for any reason, my Mongodb data store become unavailable. I implemented the following fallback code in my implementation of EntityHistoryStore:
try
{
//standard timeout for Mpongodb is 30 seconds, here we need to the system to return to the client as quickly as possible
using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(2000)))
{
await collection.InsertOneAsync(document);
}
}
catch (Exception ex)
{
logger.Error("Mongodb: " + ex.GetType(), ex);
//fallback and save to SQL Server until Mongodb is back up and running
await _fallbackEntityHistoryStore.SaveAsync(changeSet);
}
If I cannot write to Mongodb the system will fallback to my FallbackEntityHistoryStore which is simple in implementation:
public class FallbackEntityHistoryStore : IFallbackEntityHistoryStore, ITransientDependency
{
private readonly IRepository<EntityChangeSet, long> _changeSetRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public FallbackEntityHistoryStore(
IRepository<EntityChangeSet, long> changeSetRepository,
IUnitOfWorkManager unitOfWorkManager
)
{
_changeSetRepository = changeSetRepository;
_unitOfWorkManager = unitOfWorkManager;
}
public async Task SaveAsync(EntityChangeSet changeSet)
{
using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
{
await _changeSetRepository.InsertAsync(changeSet);
await uow.CompleteAsync();
}
}
}
This works well on the first call but then fails on subsequent calls with the message:
Cannot insert explicit value for identity column in table '[dbo].[AbpEntityChangeSets]' when IDENTITY_INSERT is set to OFF.
I can reset the identity_insert value in the database directly with SET IDENTITY_INSERT dbo.AbpEntityChangeSets ON
and the next iteration will work again but it sets the identity_insert value of the table back to OFF.
Is there some piece of magic which you use in your solution that I can implement in my solution? I have searched your source code on github but I can see nothing you are doing different. Googling the issue suggests I should change data annotations on the models but I don't remember changing any of your models, I built new ones to support Mongdb Bson documents.
Any pointers what to do next? Cheers, Bob
10 Answer(s)
-
0
any ideas, @aaron?
-
0
changeSet.Id
should not be set. Can you try resetting it tonull
? -
0
Thanks, Aaron, sorry I've not got back to you. I will take a closer look at this over the weekend.
-
0
Hi Aaron,
changeSet.Id = null;
Cannot convert to 'long' because it is a non-nullable value type
-
0
What is the value of
changeSet.Id
at that point? -
0
Hi Aaron, thanks for helping. The value of
changeSet.id
is 0 at that point. -
0
Hi @bobingham
Have you solved this problem ?
-
0
Nope, it's on a backburner but I do need to get back to it because I have no fallback poisition should Mongo Atlas not be available. The last time Azure reconfigured my app, changed IP's and they were not in my Mongo Atlas whitelist so I had a point of failure.
-
0
Hi @bobingham
Please reopen if this is still a problem for you.
-
0
Ok. It is still a problem and I have it on my issues list but there are more pressing issues. Re-opening this means having to get to know the entity history module all over again plus the changes you implemented at a later date. Thanks for the heads up.