间歇性FileHelpers构造函数NullReferenceException

时间:2014-10-30 16:27:45

标签: c# .net filehelpers

我突然开始使用以下堆栈获得间歇性NRE:

at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at FileHelpers.RecordInfo.RecordInfoFactory.Resolve(Type type)
at FileHelpers.RecordInfo.Resolve(Type type)
at FileHelpers.EngineBase..ctor(Type recordType, Encoding encoding)
at FileHelpers.EngineBase..ctor(Type recordType)
at FileHelpers.EventEngineBase`1..ctor(Type recordType)
at FileHelpers.FileHelperAsyncEngine`1..ctor()
...

投掷线是:

FileHelperAsyncEngine<MicrosoftAdCenterGenericKeywordReportData> engine = new FileHelperAsyncEngine<MicrosoftAdCenterGenericKeywordReportData>();

其中MicrosoftAdCenterGenericKeywordReportData为:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FileHelpers;

namespace pecom.Common.Entities
{
    [IgnoreFirst(11)]
    [IgnoreLast(1)]
    [IgnoreEmptyLines()]
    [DelimitedRecord(",")]
    public class MicrosoftAdCenterGenericKeywordReportData
    {
        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string KeywordId;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string AdGroupId;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string AdGroupName;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string AverageCpc;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string AverageCpm;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string AveragePosition;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string CampaignId;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Clicks;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string ConversionRate;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Conversions;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Spend;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Ctr;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Impressions;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string Keyword;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string QualityScore;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string TimePeriod;

        [FieldQuoted('"', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
        public string CurrentMaxCpc;
    }
}

对于使用“间歇性”的任何指示表示赞赏和道歉 - 我真的很难将这个问题打倒。这与FileHelpers 2.9.9有关。请注意,代码是通过Parallel.Invoke多线程的。我看了FileHelpers FileHelpers.RecordInfo.Resolve方法,它似乎至少设计为线程安全的(为方便起见,我在下面包含)。

非常感谢, 皮特

/// <summary>
/// Return the record information for the type
/// </summary>
/// <param name="type">Type we want settings for</param>
/// <remarks>Threadsafe</remarks>
/// <returns>Record Information (settings and functions)</returns>
public static IRecordInfo Resolve(Type type)
{
    RecordInfo res;

    lock (type) {
        lock (mRecordInfoCache) {
            if (mRecordInfoCache.TryGetValue(type, out res))
                return (IRecordInfo) res.Clone();
        }

        // class check cache / lock / check cache  and create if null algorythm

        res = new RecordInfo(type);
        lock (mRecordInfoCache) {
            if (!mRecordInfoCache.ContainsKey(type))
                mRecordInfoCache.Add(type, res);
        }

        return (IRecordInfo) res.Clone();
    }
}
}

0 个答案:

没有答案