I have a piece of code to reprocess with some logic a group of entities in an api rest service controller.
In production environment ive started to get exceptions aparently impossible in a logic sense like this one:
>Mensaje:Value cannot be null.
>Parameter name: source
>Source:System.Linq
>InnerException:
>StackTrace: at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, >Func`2 predicate)
When i tried to reproduce the error in local environment or event in production environment i figured out that this happen only randomly times making it extremly hard to reproduce it.
After lots of test i get once and just once reproduce the exception in while debuging and i found that from one line to another i get one of the entity attributes null without reason that i can understand.
In first instance i believe that maybe i was taking too much entities in only one DB context instance so i tried to make smaller list. But the error still happening. indeed with only 15 entities i still geting the exception.
This is how the code structure looks:
private static void StartProces( List<Someentitie> someentitie)
{
const int packageAmount = 200;
var index = 0;
while (indice < fichadas.Count )//sometimes are 15, sometimes are 10.000. I get the error in both cases.
{
var myEntitiePackage = someentitie.Skip(index).Take(packageAmount).Select(f => f.Id).ToList();
index += packageAmount - 1;
var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
optionsBuilder.UseSqlServer(AppSettingsHelper.ConnectionStrings.DefaultConnection)
.UseLoggerFactory(LoggingHelper.LoggerFactory);
var newDb = new MyContext(optionsBuilder.Options);
var myEntities = newDb.Entities.Where(f => myEntitiePackage.Contains(f.Id)).Include(f => f.subEntitie)
.Include(f => f.anotherSubEntitie)
.Include(f => f.AnotherSubSubEntitite).ToList();
myEntities.ForEach(f =>
{
f.ReProcess(dbNuevo);
});
}
}
public void ReProcess(MyContext db)
{
CleanAndSaveMyEntitie(db);
FillEntitieWithBasicAttributesAndSave(db)
// At this point this.importantAttribute has a value
new AuditoryLine(this, userId).Create(db);
db.SaveChanges();
// At this point this.importantAttribute is null
....
}
(Corss-posted on Stack Overflow)
This issue is lacking enough information for us to effectively reproduce. Please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing.
There is no aprent way to reproduce the bug at all.
In my environment with the original code, with the same database,, with the same everithing its not demostrable the behavior and thats precisly the point of this bug.
It makes a simple code line instable!
I've added log in production environment where i can see the entity just lose his value after db.savechanges each time this happen.
Just tell me if there is something concrete that i can give you.
DbContext is not thread safe. I believe point of issue is using ForEach with passing DbContext to ReProcess method inside. Since code snippet does not reveal whole code inside ReProcess it would be hard to tell what causes it. But SaveChanges concurrently like that are likely to cause weird errors.
Either create new DbContext inside ReProcess method or remove ForEach and iterate over each entity at a time.
Thanks! I will try it!
So i make something like this?
foreach (var e in myEntities)
{
e.ReProcess(newDb);
});
DbContext is not thread safe. I believe point of issue is using
ForEachwith passing DbContext toReProcessmethod inside. Since code snippet does not reveal whole code insideReProcessit would be hard to tell what causes it. But SaveChanges concurrently like that are likely to cause weird errors.Either create new DbContext inside
ReProcessmethod or removeForEachand iterate over each entity at a time.
I make the foreach change, but still happening the exception
@ufalak We're likely going to need a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. I understand that this may not be easy, but realistically it's the only way we can make progress here.
A novelty on this is that also with only one entity also im having this issue.
Thats mean that this doesnt have any relation with possible multiple threads or memory overloading or something like that.
@ufalak We're likely going to need a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. I understand that this may not be easy, but realistically it's the only way we can make progress here.
Maybe can help if i put a piece of log tracking all of this?
@ufalak then please post a project that demonstrates the issue
Ok i will post the method that make this happend. Its a bit long.
Maybe it will be useful to you if i post the log file where you can see the object attribute state and db queries?
@ufalak We need to debug your code to figure out what is going on. We are unlikely to be able to help you until you post code that we can _run_.
Ok so, close the issue.
Its imposible to me to post the entire runable code.
One reason is that is huge all of the logic, and model entitites that i need to post to make it runnable.
Another is because of the confidentiallity with my client
I can give you pieces of code (not runnable) or log info.
But runnable code, imposible.