<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NServiceBus;

namespace NES.NServiceBus
{
    public class CommandContextProvider : ICommandContextProvider
    {
        private static readonly Dictionary<Type, Func<object, Guid>> _cache = new Dictionary<Type, Func<object, Guid>>();
        private static readonly object _cacheLock = new object();
        private readonly IBus _bus;

        public CommandContextProvider(IBus bus)
        {
            _bus = bus;
        }

        public CommandContext Get()
        {
            var command = ExtensionMethods.CurrentMessageBeingHandled;
            var commandType = command.GetType();

            lock (_cacheLock)
            {
                Func<object, Guid> property;

                if (!_cache.TryGetValue(commandType, out property))
                {
                    var propertyInfo = commandType.GetProperty("Id");

                    if (propertyInfo != null)
                    {
                        var commandParameter = Expression.Parameter(typeof(object), "command");
                        var propertyCall = Expression.Property(Expression.Convert(commandParameter, commandType), propertyInfo);

                        property = Expression.Lambda<Func<object, Guid>>(propertyCall, commandParameter).Compile();
                    }
                    else
                    {
                        property = c => GuidComb.NewGuidComb();
                    }

                    _cache[commandType] = property;
                }

                return new CommandContext
                {
                    Id = property(command),
                    Headers = _bus.CurrentMessageContext.Headers.Where(h => !h.Key.StartsWith("NServiceBus")).ToDictionary(h => h.Key, h => (object)h.Value)
                };
            }
        }
    }
}<commit_msg>excluded CorrId and WinIdName headers from CommandContext<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using NServiceBus;

namespace NES.NServiceBus
{
    public class CommandContextProvider : ICommandContextProvider
    {
        private static readonly Dictionary<Type, Func<object, Guid>> _cache = new Dictionary<Type, Func<object, Guid>>();
        private static readonly object _cacheLock = new object();
        private readonly IBus _bus;

        public CommandContextProvider(IBus bus)
        {
            _bus = bus;
        }

        public CommandContext Get()
        {
            var command = ExtensionMethods.CurrentMessageBeingHandled;
            var commandType = command.GetType();

            lock (_cacheLock)
            {
                Func<object, Guid> property;

                if (!_cache.TryGetValue(commandType, out property))
                {
                    var propertyInfo = commandType.GetProperty("Id");

                    if (propertyInfo != null)
                    {
                        var commandParameter = Expression.Parameter(typeof(object), "command");
                        var propertyCall = Expression.Property(Expression.Convert(commandParameter, commandType), propertyInfo);

                        property = Expression.Lambda<Func<object, Guid>>(propertyCall, commandParameter).Compile();
                    }
                    else
                    {
                        property = c => GuidComb.NewGuidComb();
                    }

                    _cache[commandType] = property;
                }

                return new CommandContext
                {
                    Id = property(command),
                    Headers = _bus.CurrentMessageContext.Headers
                        .Where(h =>
                            !h.Key.Equals("CorrId", StringComparison.InvariantCultureIgnoreCase) &&
                            !h.Key.Equals("WinIdName", StringComparison.InvariantCultureIgnoreCase) &&
                            !h.Key.StartsWith("NServiceBus", StringComparison.InvariantCultureIgnoreCase))
                        .ToDictionary(h => h.Key, h => (object)h.Value)
                };
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08621506395073425, 'fraction_numerical': 0.0, 'mean_word_length': 1.6148606811145512, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '405202', 'n_tokens_mistral': 1009, 'n_tokens_neox': 935, 'n_words': 207}