Note that there are some explanatory texts on larger screens.

plurals
  1. POParallel For Not Working With EF4.1 and Automapper
    primarykey
    data
    text
    <p>I'm having problems with implementing a Parallel For it seems to cause error messages intermittently.</p> <p>I am trying to speed up the process of mapping a complex ViewModel that is built using lots of navigation proprieties etc. The code below is a simplified non parallel version.</p> <pre><code>var Model = MyRepository.All.AsEnumerable().Select(a =&gt; Mapper.Map&lt;Model, ViewModel&gt;(a)); return View(Model); </code></pre> <p>This works fine and I never get any errors. Knowing that my ViewModel mapping was complex I decided to test a parallel version to see if it was faster. Simplified version is:</p> <pre><code>var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount}; ConcurrentBag&lt;ViewModel&gt; ViewModel = new ConcurrentBag&lt;ViewModel&gt;(); Parallel.ForEach(Model, options, dr =&gt; ViewModel.Add(Mapper.Map&lt;Model,ViewModel&gt;(dr))); var ViewModelSorted = ViewModel.AsEnumerable().OrderBy(a =&gt; a.SortDate); return View(ViewModelSorted); </code></pre> <p>It often completes and displays results in half the time. So it is clearly faster. However I now sometimes get error messages about null reference exceptions etc in some of my partial entity class methods. These errors seem intermittent even when I test the same data. I don't really understand why? The code doesn't change or update DB etc and nothing else is updating the DB while I run the code. Is it just not possible to use a Parallel For in this situation?</p> <p>Update my error message is :</p> <pre><code>{"Object reference not set to an instance of an object."} </code></pre> <p>Stack trace:</p> <pre><code> at SpotList.Domain.Entities.Vessel.GetNextFixture(fixture fixture) in C:\Users\Graeme\documents\visual studio 2010\Projects\SpotList\Domain\Entities\Vessel.cs:line 47 at SpotList.WebUI.Infrastructure.AutoMap.Charterer2.ResolveCore(Vessel source) in C:\Users\Graeme\documents\visual studio 2010\Projects\SpotList\SpotList\Infrastructure\AutoMap\AutoMapperBootstrapper.cs:line 401 at AutoMapper.ValueResolver`2.Resolve(ResolutionResult source) at AutoMapper.DeferredInstantiatedResolver.Resolve(ResolutionResult source) at AutoMapper.PropertyMap.ResolveValue(ResolutionContext context) at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) </code></pre> <p>The error line corresponds to the code here:</p> <pre><code> public fixture GetNextFixture(fixture fixture) { fixtureperiod fixtureperiod = fixture.GetMostRecentFixturePeriod(); </code></pre> <p>So fixture is null however the same thing never seems to occur if I run the non-parallel version</p> <p>Thanks</p> <p>Graeme</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload