diff --git a/Rai.PacketMediator/PacketDistributor.cs b/Rai.PacketMediator/PacketDistributor.cs index e1861b3..6987afc 100644 --- a/Rai.PacketMediator/PacketDistributor.cs +++ b/Rai.PacketMediator/PacketDistributor.cs @@ -4,7 +4,6 @@ using System.Collections.Concurrent; using System.Collections.Immutable; using System.Reflection; using System.Threading.Channels; -using DotNext.Collections.Generic; using DotNext.Linq.Expressions; using DotNext.Metaprogramming; using Microsoft.Extensions.DependencyInjection; @@ -47,16 +46,16 @@ public class PacketDistributor where TPacketIdEnum : En } var tempDeserializationMap = - new Dictionary>(); + new ConcurrentDictionary>(); _packetHandlersInstantiation = new ConcurrentDictionary?>(); - packetHandlers.ForEach(x => + Parallel.ForEach(packetHandlers, packetHandlerPair => { var packetHandler = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, - x.Value); - _packetHandlersInstantiation.TryAdd(x.Key, packetHandler as IPacketHandler); + packetHandlerPair.Value); + _packetHandlersInstantiation.TryAdd(packetHandlerPair.Key, packetHandler as IPacketHandler); }); - foreach (var packetsType in packetDictionary) + Parallel.ForEach(packetDictionary, packetsType => { var lambda = CodeGenerator.Lambda>(fun => { @@ -69,8 +68,8 @@ public class PacketDistributor where TPacketIdEnum : En CodeGenerator.Return(packetVariable); }).Compile(); - tempDeserializationMap.Add(packetsType.Key, lambda); - } + tempDeserializationMap.TryAdd(packetsType.Key, lambda); + }); _deserializationMap = tempDeserializationMap.ToImmutableDictionary(); }