<commit_before>﻿// ReSharper disable CheckNamespace
namespace Owin
// ReSharper restore CheckNamespace
{
    using System;
    using System.Threading;

    using Nancy.Owin;

    /// <summary>
    /// OWIN extensions for Nancy
    /// </summary>
    public static class Extensions
    {
        public static IAppBuilder UseNancy(this IAppBuilder builder, NancyOptions options = null)
        {
            var nancyOptions = options ?? new NancyOptions();

            var appDisposing = builder.Properties["host.OnAppDisposing"] as CancellationToken?;

            if (appDisposing.HasValue)
            {
                appDisposing.Value.Register(nancyOptions.Bootstrapper.Dispose);
            }

            return builder.Use(typeof(NancyOwinHost), nancyOptions);
        }

        public static IAppBuilder UseNancy(this IAppBuilder builder, Action<NancyOptions> configuration)
        {
            var options = new NancyOptions();
            configuration(options);
            return UseNancy(builder, options);
        }
    }
}<commit_msg>Made Owin dispose hook more defensive, fixes #1295<commit_after>﻿// ReSharper disable CheckNamespace
namespace Owin
// ReSharper restore CheckNamespace
{
    using System;
    using System.Threading;

    using Nancy.Owin;

    /// <summary>
    /// OWIN extensions for Nancy
    /// </summary>
    public static class Extensions
    {
        private const string AppDisposingKey = "host.OnAppDisposing";

        public static IAppBuilder UseNancy(this IAppBuilder builder, NancyOptions options = null)
        {
            var nancyOptions = options ?? new NancyOptions();

            HookDisposal(builder, nancyOptions);

            return builder.Use(typeof(NancyOwinHost), nancyOptions);
        }

        private static void HookDisposal(IAppBuilder builder, NancyOptions nancyOptions)
        {
            if (!builder.Properties.ContainsKey(AppDisposingKey))
            {
                return;
            }

            var appDisposing = builder.Properties[AppDisposingKey] as CancellationToken?;

            if (appDisposing.HasValue)
            {
                appDisposing.Value.Register(nancyOptions.Bootstrapper.Dispose);
            }
        }

        public static IAppBuilder UseNancy(this IAppBuilder builder, Action<NancyOptions> configuration)
        {
            var options = new NancyOptions();
            configuration(options);
            return UseNancy(builder, options);
        }
    }
}
{'fraction_non_alphanumeric': 0.08065810593900481, 'fraction_numerical': 0.0016051364365971107, 'mean_word_length': 2.216774193548387, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '4526427', 'n_tokens_mistral': 645, 'n_tokens_neox': 628, 'n_words': 156}