NodaTime TypeInitializationException Только устройство, только выпуск

Я получаю TypeInitializationException, используя NodaTime, но только в Release и только на устройствах.

Вот трассировка стека:

System.TypeInitializationException: The type initializer for 'Patterns' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.Text.ZonedDateTimePattern' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.DateTimeZone' threw an exception. ---> System.TypeInitializationException: The type initializer for 'NodaTime.Text.OffsetPattern' threw an exception. ---> System.Resources.MissingManifestResourceException: Exception of type 'System.Resources.MissingManifestResourceException' was thrown.
   at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
   at NodaTime.Globalization.NodaFormatInfo.get_OffsetPatternLong()
   at NodaTime.Text.OffsetPatternParser.ParsePartialPattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.CreateGeneralPattern(NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.ParsePartialPattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPatternParser.ParsePattern(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.FixedFormatInfoPatternParser`1.<>c__DisplayClass0.<.ctor>b__2(String patternText)
   at NodaTime.Utility.Cache`2.GetOrAdd(TKey key)
   at NodaTime.Text.OffsetPattern.Create(String patternText, NodaFormatInfo formatInfo)
   at NodaTime.Text.OffsetPattern..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.TimeZones.FixedDateTimeZone.MakeId(Offset offset)
   at NodaTime.DateTimeZone.BuildFixedZoneCache()
   at NodaTime.DateTimeZone..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.LocalDateTime.InUtc()
   at NodaTime.Text.ZonedDateTimePattern..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.Text.ZonedDateTimePattern.CreateWithInvariantCulture(String patternText, IDateTimeZoneProvider zoneProvider)
   at NodaTime.Text.ZonedDateTimePattern.Patterns..cctor()
   --- End of inner exception stack trace ---
   at NodaTime.ZonedDateTime.ToString(String patternText, IFormatProvider formatProvider)
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
   at System.Text.StringBuilder.AppendFormat(String format, Object[] args)
   at Models.EventSummary.get_DisplayDate()

person Jeff    schedule 11.10.2014    source источник
comment
Хм. Интересный. Какова ваша культура? Я ожидаю, что он просто вернется к любой культуре, которую я указал как культуру для сборки ... не похоже, что есть какие-либо другие культуры с этим ресурсом. Хм.   -  person Jon Skeet    schedule 11.10.2014
comment
Когда вы говорите, что это только в выпуске - предположительно, вы используете одну и ту же сборку Noda Time в обоих случаях, установленную через NuGet?   -  person Jon Skeet    schedule 11.10.2014
comment
Да, все настраивается через NuGet.   -  person Jeff    schedule 11.10.2014
comment
Итак, NodaTime.dll должен быть одинаковым как при выпуске, так и при отладке. Похоже, это вполне может быть дубликатом stackoverflow.com/questions/26140155.   -  person Jon Skeet    schedule 11.10.2014
comment
А также github.com/Reactive-Extensions/Rx.NET/issues/13 - похоже, это проблема с фреймворком, а не с Noda Time, что будет сложно исправить :(   -  person Jon Skeet    schedule 11.10.2014
comment
Судя по другим комментариям в Твиттере, похоже, что resx не поддерживается в WP8.1, тогда как resw поддерживается. Мне нужно исследовать это немного дальше... что будет больно, если для этого потребуется физическое устройство :(   -  person Jon Skeet    schedule 20.10.2014
comment
@JonSkeet Я напрямую получаю MissingManifestResourceException. Он выбрасывается в NodaFormatInfo при попытке загрузить ресурс OffsetPatternLong.   -  person Markus Bruckner    schedule 31.10.2014
comment
Ничего, только что наткнулся на qaster.com/q/520694969783578625.   -  person Markus Bruckner    schedule 31.10.2014


Ответы (1)


обходной путь Фила Хоффа решил эту проблему для нас. Создайте следующий класс WindowsRuntimeResourceManager:

/// <summary>
/// from http://blogs.msdn.com/b/philliphoff/archive/2014/11/19/missingmanifestresourceexception-when-using-portable-class-libraries-in-winrt.aspx
/// </summary>
public class WindowsRuntimeResourceManager : ResourceManager
{
    private readonly ResourceLoader _resourceLoader;

    private WindowsRuntimeResourceManager(string baseName, Assembly assembly)
        : base(baseName, assembly)
    {
        _resourceLoader = ResourceLoader.GetForViewIndependentUse(baseName);
    }

    public static void InjectIntoResxGeneratedApplicationResourcesClass(Type resxGeneratedApplicationResourcesClass)
    {
        resxGeneratedApplicationResourcesClass.GetRuntimeFields()
          .First(m => m.Name == "resourceMan")
          .SetValue(null, new WindowsRuntimeResourceManager(resxGeneratedApplicationResourcesClass.FullName, resxGeneratedApplicationResourcesClass.GetTypeInfo().Assembly));
    }

    public override string GetString(string name, CultureInfo culture)
    {
        return _resourceLoader.GetString(name);
    }
}

Перед использованием NodaTime (например, в конструкторе приложения) замените диспетчеры ресурсов сборки NodaTime:

        Assembly nodaTimeAssembly = typeof(LocalDate).GetTypeInfo().Assembly;

        Type messagesResource = nodaTimeAssembly.GetType("NodaTime.Properties.Messages");
        Type patternResource = nodaTimeAssembly.GetType("NodaTime.Properties.PatternResources");

        WindowsRuntimeResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(messagesResource);
        WindowsRuntimeResourceManager.InjectIntoResxGeneratedApplicationResourcesClass(patternResource);
person Markus Bruckner    schedule 04.03.2015