EF Core 6 - The instance of entity type 'AppCase' cannot be tracked because another instance with the same key value for {} is already being tracked - asp.net-core

I'm using ASP.NET Core 6, and EF Core 6, and Repository/UnitOfWork pattern. I have some trouble with my project, The error "The instance of entity type 'AppCase' cannot be tracked because another instance with the same key value for {} is already being tracked" have throwed when I call UpdateAsync in Repository.
I think i know the issue description, and I used AsNoTracking() but the issue can't have been solved yet.
How can I solve it, please.
Thanks for your support and sorry for my bad English!
Here is my source code:
//MicroOrderBusiness.cs
public async Task<dynamic> CreateOrder(MicroOrderRequest requsetObject, string caseNo)
{
string processCode = "CEC1D433-C376-498B-8A80-B23DC3772024";
var caseModel = await _appCaseBusiness.PrepareAppCase(processCode, caseNo);
if (caseModel == null)
throw new Exception("Create request faield!");
var appCaseEntity = await _appCaseBusiness.AddNewCase(caseModel); // => Add new Case by using EF 6 Core via Repository and UnitOfWork**
if (appCaseEntity == null)
throw new Exception("Adding faield!");
var taskResponse = await _processTaskBusiness.ExecuteTask<MicroOrderRequest>(caseModel.FirstTaskCode, requsetObject);
if (Equals(taskResponse, null))
throw new Exception("Response task is not found!");
caseModel.CaseObjects.Add(taskResponse);
// Update case state
appCaseEntity.State = STATE.DONE;
appCaseEntity.CaseObject = JsonConvert.SerializeObject(appCase.CaseObjects);
var updateCase = await _appCaseRepository.UpdateAsync(appCaseEntity); // => Error throw here**
if (updateCase == null)
throw new Exception($"Update state case faiedl!");
return caseModel;
}
//Program.cs
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseOracle(connectionString,
o => o.MigrationsAssembly(typeof(AppDbContext)
.Assembly.FullName)
.UseOracleSQLCompatibility("12")));
builder.Services.AddScoped(typeof(IUnitOfWork), typeof(AppDbContext));

The error has been explained in the document:https://learn.microsoft.com/en-us/ef/core/change-tracking/identity-resolution#attaching-a-serialized-graph
I checked your codes, I think the error may caused by:
tracking a serialized graph of entities
appCaseEntity.CaseObject = JsonConvert.SerializeObject(appCase.CaseObjects);
var updateCase = await _appCaseRepository.UpdateAsync(appCaseEntity); // => Error throw here**
You could learn more details and found the solution in this part
reusing a DbContext instance for multiple units-of-work
var appCaseEntity = await _appCaseBusiness.AddNewCase(caseModel);
........
var updateCase = await _appCaseRepository.UpdateAsync(appCaseEntity);
You registed the dbcontext as below(the lifetime is scoped by default):
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseOracle(connectionString,
o => o.MigrationsAssembly(typeof(AppDbContext)
.Assembly.FullName)
.UseOracleSQLCompatibility("12")));
You could try to set the lifetime with Trasident or Use DbContextFactory to create different instance in different units of work
builder.Services.AddDbContext<AppDbContext>((options=>........),ServiceLifetime.Transient)
Check this document about using DbContextFactory
If you have further issue on this case,please provide the minimal codes that could reproduce the error

Related

Using UserManager not working inside Timer

In my project I am trying to get a user based on it's email adress every second with the UserManager but when I do this I get the following error Cannot access a disposed object Object name: 'UserManager1, but this is when I do it inside of a Timer(). If I just do it once there is no problem, how can I fix this? This timer is inside a class that is being called by a SignalR Hub.
Code:
Timer = new System.Threading.Timer(async (e) =>
{
IEnumerable<Conversation> conversations = await _conversationsRepo.GetAllConversationsForUserEmailAsync(userMail);
List<TwilioConversation> twilioConversations = new List<TwilioConversation>();
foreach (Conversation conversation in conversations)
{
TwilioConversation twilioConversation = await _twilioService.GetConversation(conversation.TwilioConversationID);
twilioConversation.Messages = await _twilioService.GetMessagesForConversationAsync(conversation.TwilioConversationID);
twilioConversation.ParticipantNames = new List<string>();
List<TwilioParticipant> participants = await _twilioService.GetParticipantsForConversationAsync(conversation.TwilioConversationID);
foreach (TwilioParticipant participant in participants)
{
User user = await _userManager.FindByEmailAsync(participant.Email);
twilioConversation.ParticipantNames.Add(user.Name);
}
twilioConversations.Add(twilioConversation);
}
}, null, startTimeSpan, periodTimeSpan);
UserManager along with quite a few other types is a service that has a scoped lifetime. This means that they are only valid within the lifetime of a single request.
That also means that holding on to an instance for longer is not a safe thing to do. In this particular example, UserManager depends on the UserStore which has a dependency on a database connection – and those will definitely be closed when the request has been completed.
If you need to run something outside of the context of a request, for example in a background thread, or in your case in some timed execution, then you should create a service scope yourself and retrieve a fresh instance of the dependency you rely on.
To do that, inject a IServiceScopeFactory and then use that to create the scope within your timer code. This also applies to all other scoped dependencies, e.g. your repository which likely requires a database connection as well:
Timer = new System.Threading.Timer(async (e) =>
{
using (var scope = serviceScopeFactory.CreateScope())
{
var conversationsRepo = scope.ServiceProvider.GetService<ConversionsRepository>();
var userManager = scope.ServiceProvider.GetService<UserManager<User>>();
// do stuff
}
}, null, startTimeSpan, periodTimeSpan);

How to perform async ModelState validation with FluentValidation in Web API using .NET Core

This question is a follow up to this post - How to perform async ModelState validation with FluentValidation in Web API?.
I was wondering if FluentValidation has a way to perform async ModelState validation in .net core web api. I have a FluentValidation Validator class which contains async validation methods such as "MustAsync", which means in my business service class I call the validator manually using "ValidateAsync". I also want to use this same validator class to validate the model coming in from the request. I went through the documents and read that the only way to do this is to manually call the "ValidateAsync()" method since the .net pipeline is synchronous. I would rather not manually have to call this method from within my controller, I would prefer to either register it in the startup (have the framework automatically call the the validator on my model) or decorate my request model with the validator.
Has anyone been able to achieve this?
Thanks!
Based on the linked question, I've adapted the code slightly to be compatible with ASP.NET Core (2.2, in my case). In general, this is using the IAsyncActionFilter interface. You can read about it in the official docs.
public class ModelValidationActionFilter : IAsyncActionFilter
{
private readonly IValidatorFactory _validatorFactory;
public ModelValidationActionFilter(IValidatorFactory validatorFactory) => _validatorFactory = validatorFactory;
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var allErrors = new Dictionary<string, object>();
// Short-circuit if there's nothing to validate
if (context.ActionArguments.Count == 0)
{
await next();
return;
}
foreach (var (key, value) in context.ActionArguments)
{
// skip null values
if (value == null)
continue;
var validator = _validatorFactory.GetValidator(value.GetType());
// skip objects with no validators
if (validator == null)
continue;
// validate
var result = await validator.ValidateAsync(value);
// if it's valid, continue
if (result.IsValid) continue;
// if there are errors, copy to the response dictonary
var dict = new Dictionary<string, string>();
foreach (var e in result.Errors)
dict[e.PropertyName] = e.ErrorMessage;
allErrors.Add(key, dict);
}
if (allErrors.Any())
{
// Do anything you want here, if the validation failed.
// For example, you can set context.Result to a new BadRequestResult()
// or implement the Post-Request-Get pattern.
}
else
await next();
}
}
If you want to apply this filter globally, you can add the filter to the AddMvc call in your Startup class. For example:
services.AddMvc(options =>
{
options.Filters.Add<ModelValidationActionFilter>();
// uncomment the following line, if you want to disable the regular validation
// options.ModelValidatorProviders.Clear();
});
I had trouble getting the code in #nachtjasmin's answer to work with newer versions of FluentValidation. Specifically, the trouble is that ValidateAsync now takes an IValidationContext instead of the model being validated, and the context can't be created without knowing the type of the model at compile time.
Eventually I stumbled upon this answer, which points out that the exact type is not important and uses object instead.
So, instead of:
var result = await validator.ValidateAsync(value);
You can use:
var context = new ValidationContext<object>(value);
var result = await validator.ValidateAsync(context);
Based on the answer above by #nachtjasmin, you can add this in two ways,
Using AddMvc
services.AddControllersWithViews(options =>
{
options.Filters.Add<FluentValidationActionFilter>();
});
Using AddControllersWithViews
services.AddControllersWithViews(options =>
{
options.Filters.Add<FluentValidationActionFilter>();
});
If your's is just a Web API and you don't have any Razor pages involved, then you can consider using AddControllersWithViews over AddMvc, as the AddMvc uses the AddControllersWithViews internally and add the services.AddRazorPages() on top of that.
You can see this info here for AddMvc and here for AddControllersWithViews

ActiveRecord 3 RC 1 with NHibernate 3.2 causes an unexpected exception

Since I felt adventurous the other day I decided compiling ActiveRecord 3 RC 1 with NHibernate 3.2 and see what happens.
Besides the breaking changes which I fixed I encountered a very strange behavior regarding SessionScopes and Linq queries.
Usually I don't have to use a session scope when using a Linq query but after I compiled ActiveRecord 3 RC 1 with NHibernate 3.2 I got the following error:
Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.
Stack Trace: at Castle.ActiveRecord.Framework.ActiveRecordLinqBase`1.get_Queryable()
at Castle.ActiveRecord.Framework.ActiveRecordLinq.AsQueryable[T]()
at Danel.Nursing.Scheduling.Actions.DataServices.BranchDataService.GetBranches() in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling.Actions\DataServices\BranchDataService.cs:line 21
at Danel.Nursing.Scheduling.Controllers.SmallHoursAmountController.<>c__DisplayClassb.<SetBranches>b__a() in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling\Controllers\SmallHoursAmountController.cs:line 275
at Danel.Nursing.Scheduling.Viewlets.WaitForAction.Worker_DoWork(Object sender
DoWorkEventArgs e) in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling\Viewlets\WaitForAction.cs:line 40
It seems that the error comes from here:
public class ActiveRecordLinqBase<T> : ActiveRecordBase<T>
{
public static IOrderedQueryable<T> Queryable
{
get
{
var activeScope = holder.ThreadScopeInfo.GetRegisteredScope(); // The registered scope is null...
if (activeScope == null)
throw new ActiveRecordException("Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.");
var key = holder.GetSessionFactory(typeof(T));
var session = activeScope.IsKeyKnown(key) ? activeScope.GetSession(key) : SessionFactoryHolder.OpenSessionWithScope(activeScope, key);
return session.AsQueryable<T>();
}
}
}
What has changed that now I have to open a new SessionScope?
I had some trouble too with the Queryable function. Although I did not have the sessions scope problem, I had trouble saving update to objects retrieved by IQueryable. It seems that the new session was never registered with the active scope. I also changed the scope retrieval so maybe this also helps for you:
public new IOrderedQueryable<T> Queryable
{
get
{
ISessionFactory key = ActiveRecordMediator<T>.GetSessionFactoryHolder().GetSessionFactory(typeof(T));
ISessionScope activeScope = SessionScope.Current;
if (activeScope == null)
throw new ActiveRecordException("Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.");
var session = activeScope.IsKeyKnown(key) ? activeScope.GetSession(key) : OpenSessionWithScope(activeScope, key);
if (!activeScope.IsKeyKnown(key))
{
activeScope.RegisterSession(key,session);
}
return session.AsQueryable<T>();
}
}

What permissions do I need to grant to run RavenDB in Server mode?

I'm reading through Rob Ashton's excellent blog post on RavenDB:
http://codeofrob.com/archive/2010/05/09/ravendb-an-introduction.aspx
and I'm working through the code as I read. But when I try to add an index, I get a 401 error. Here's the code:
class Program
{
static void Main(string[] args)
{
using (var documentStore = new DocumentStore() { Url = "http://localhost:8080" })
{
documentStore.Initialise();
documentStore.DatabaseCommands.PutIndex(
"BasicEntityBySomeData",
new IndexDefinition<BasicEntity, BasicEntity>()
{
Map = docs => from doc in docs
where doc.SomeData != null
select new
{
SomeData = doc.SomeData
},
});
string entityId;
using (var documentSession = documentStore.OpenSession())
{
var entity = new BasicEntity()
{
SomeData = "Hello, World!",
SomeOtherData = "This is just another property",
};
documentSession.Store(entity);
documentSession.SaveChanges();
entityId = entity.Id;
var loadedEntity = documentSession.Load<BasicEntity>(entityId);
Console.WriteLine(loadedEntity.SomeData);
var docs = documentSession.Query<BasicEntity>("BasicEntityBySomeData")
.Where("SomeData:Hello~")
.WaitForNonStaleResults()
.ToArray();
docs.ToList().ForEach(doc => Console.WriteLine(doc.SomeData));
Console.Read();
}
}
}
It throws the 401 error when on the line that makes the PutIndex() call. Any ideas what permissions I need to apply? And where I need to apply them?
What do you mean by Server mode? Do you mean simply executing Raven.Server?
I've not had to do anything special client-side to get that to work, although I have had to run Raven.Server with elevated privileges because I'm not sure the code to ask for relevant permissions is quite working as intended. (Actually, I'll raise a query about that on the mailing list)
You shouldn't be getting a 401 error unless you've changed the configuration of Raven.Server.
If you're running the server, you can browse to it directly using the url specified in configuration (localhost:8080 by default) - make sure it's actually running and working as intended before continuing with troubleshooting

Rhino AutoMocker and Stubs

I am using Joshua Flanagan article “Auto mocking Explained” as a guide. In the article there is a section called “The same tests, with automocker would look like this”. I used this information to build code to run the automocker.
As you can see below answer is a list returned from the BLL. Answer does have one row in it; however, all fields are null. So the test for boo fails. Any tips and hints would be greatly appreciated.
[Test]
public void GetStaffListAndRolesByTeam_CallBLLWithDALStub()
{
// Build List<> data for stub
List<StaffRoleByTeamCV> stubData = new List<StaffRoleByTeamCV>();
StaffRoleByTeamCV stubRow = new StaffRoleByTeamCV();
stubRow.Role = "boo";
stubRow.StaffId = 12;
stubRow.StaffName = "Way Cool";
stubData.Add(stubRow);
// create the automocker
var autoMocker = new RhinoAutoMocker<PeteTestBLL>();
// get instance of test class (the BLL)
var peteTestBllHdl = autoMocker.ClassUnderTest;
// stub out call to DAL inside of BLL
autoMocker.Get<IPeteTestDAL>().Stub(c => c.GetStaffListAndRolesByTeam("4146")).Return(stubData);
// make call to BLL this should return stubData
List<StaffRoleByTeamCV> answer = peteTestBllHdl.GetStaffListAndRolesByTeam("4146");
// do simple asserts to test stubData present
// this passes
Assert.IsTrue(1 == answer.Count, "Did not find any rows");
// this fails
Assert.IsTrue(answer[0].Role == "boo", "boo was not found");
}
I tried using MockMode.AAA but still no joy
An new version of AutoMocker (1.0.3) is available. The new version supports relay mode as in this example..
[TestMethod]
public void ShouldSupportOrderedTest()
{
//Arrange
var autoMocker = new RhinoAutoMocker<CustomerUpdater>();
var mockRepository = autoMocker.Repository;
using (mockRepository.Ordered())
{
autoMocker.Get<ICustomerDataProvider>().Expect(x => x.GetCustomer(Arg<int>.Is.Anything)).Return(new CustomerItem());
autoMocker.Get<ICustomerDataProvider>().Expect(x => x.UpdateCustomer(Arg<CustomerItem>.Is.Anything));
autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
autoMocker.Get<ILogWriter>().Expect(x => x.Write(Arg<string>.Is.Anything));
autoMocker.Get<IMailSender>().Expect(x => x.SendMail(Arg<string>.Is.Anything, Arg<string>.Is.Anything));
}
//Act
autoMocker.ClassUnderTest.UpdateCustomerName(1, "Frank", "frank#somecompany.com");
//Assert
ExceptionAssert.Throws<ExpectationViolationException>(mockRepository.VerifyAll,"IMailSender.SendMail(anything, anything); Expected #1, Actual #0.\r\nILogWriter.Write(anything); Expected #1, Actual #0.\r\n");
}
I haven't tried, but this article suggests that by default all the mocks created by automocker are not replayed:
http://www.lostechies.com/blogs/joshuaflanagan/archive/2008/09/25/arrange-act-assert-with-structuremap-rhinoautomocker.aspx
Yes that was true for the previous version. But was changed to support ordered tests in version 1.0.3.