<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Autofac;
using ProjectExtensions.Azure.ServiceBus.Serialization;
using Microsoft.AzureCAT.Samples.TransientFaultHandling;

namespace ProjectExtensions.Azure.ServiceBus {

    /// <summary>
    /// Class used for configuration
    /// </summary>
    public class BusConfiguration : ProjectExtensions.Azure.ServiceBus.IBusConfiguration {

        static object lockObject = new object();
        static BusConfiguration configuration;

        IContainer container;
        List<Assembly> registeredAssemblies = new List<Assembly>();
        List<Type> registeredSubscribers = new List<Type>();

        /// <summary>
        /// ctor
        /// </summary>
        internal BusConfiguration(IContainer container = null) {
            MaxThreads = 1;
            TopicName = "pro_ext_topic";
            this.container = container;
        }

        /// <summary>
        /// The Service Bus
        /// </summary>
        public IBus Bus {
            get {
                return container.Resolve<IBus>();
            }
        }

        /// <summary>
        /// The IOC Container
        /// </summary>
        public static IContainer Container {
            get {
                return configuration.container;
            }
        }

        /// <summary>
        /// DefaultSerializer
        /// </summary>
        public IServiceBusSerializer DefaultSerializer {
            get {
                return container.Resolve<IServiceBusSerializer>();
            }
        }

        /// <summary>
        /// Instance of BusConfiguration
        /// </summary>
        public static BusConfiguration Instance {
            get {
                //TODO should this throw an exception if it was not created?
                return configuration;
            }
        }

        /// <summary>
        /// Max Threads to call the message handlers from the bus messages being received
        /// </summary>
        public byte MaxThreads {
            get;
            internal set;
        }

        /// <summary>
        /// List of RegisteredAssemblies
        /// </summary>
        public IList<Assembly> RegisteredAssemblies {
            get {
                return this.registeredAssemblies;
            }
        }

        /// <summary>
        /// List of RegisteredSubscribers
        /// </summary>
        public IList<Type> RegisteredSubscribers {
            get {
                return this.registeredSubscribers;
            }
        }

        /// <summary>
        /// ServiceBusApplicationId
        /// </summary>
        public string ServiceBusApplicationId {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusNamespace
        /// </summary>
        public string ServiceBusNamespace {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusIssuerName
        /// </summary>
        public string ServiceBusIssuerName {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusIssuerKey
        /// </summary>
        public string ServiceBusIssuerKey {
            get;
            internal set;
        }

        /// <summary>
        /// ServicePath
        /// </summary>
        public string ServicePath {
            get;
            internal set;
        }

        /// <summary>
        /// TopicName
        /// </summary>
        public string TopicName {
            get;
            internal set;
        }

        internal void Configure(ContainerBuilder builder) {
            Guard.ArgumentNotNull(builder, "builder");
            if (string.IsNullOrWhiteSpace(ServiceBusApplicationId)) {
                throw new ApplicationException("ApplicationId must be set.");
            }

            builder.Register<AzureBus>(item => new AzureBus(this)).As<IBus>().SingleInstance();

            builder.RegisterType<AzureBusReceiver>().As<IAzureBusReceiver>().SingleInstance();
            builder.RegisterType<AzureBusSender>().As<IAzureBusSender>().SingleInstance();

            if (container == null) {
                container = builder.Build();
            }
            else {
                builder.Update(container);
            }

            //Set the Bus property so that the receiver will register the end points
            var prime = this.Bus;
        }

        /// <summary>
        /// Get the settings builder optionally passing in your existing IOC Container
        /// </summary>
        /// <param name="container">Your optional existing IOC container.</param>
        /// <returns></returns>
        public static BusConfigurationBuilder WithSettings() {
            return WithSettings(null);
        }

        /// <summary>
        /// Get the settings builder optionally passing in your existing IOC Container
        /// </summary>
        /// <param name="container">Your optional existing IOC container. Use <see cref="WithSettings()"/> if you do not have an existing container.</param>
        /// <returns></returns>
        public static BusConfigurationBuilder WithSettings(IContainer container) {
            Guard.ArgumentNotNull(container, "container");
            if (configuration == null) {
                lock (lockObject) {
                    if (configuration == null) {
                        configuration = new BusConfiguration(container);
                    }
                }
            }
            var builder = new ContainerBuilder();
            //last one in wins so if one is registered it will be called.
            builder.RegisterType<JsonServiceBusSerializer>().As<IServiceBusSerializer>().SingleInstance();
            return new BusConfigurationBuilder(builder, configuration);
        }

        internal void AddRegisteredAssembly(Assembly value) {
            Guard.ArgumentNotNull(value, "value");
            if (!this.registeredAssemblies.Contains(value)) {
                this.registeredAssemblies.Add(value);
            }
        }

        internal void AddRegisteredSubscriber(Type value) {<commit_msg>BUG. We don't need a container. We will create one.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Autofac;
using ProjectExtensions.Azure.ServiceBus.Serialization;
using Microsoft.AzureCAT.Samples.TransientFaultHandling;

namespace ProjectExtensions.Azure.ServiceBus {

    /// <summary>
    /// Class used for configuration
    /// </summary>
    public class BusConfiguration : ProjectExtensions.Azure.ServiceBus.IBusConfiguration {

        static object lockObject = new object();
        static BusConfiguration configuration;

        IContainer container;
        List<Assembly> registeredAssemblies = new List<Assembly>();
        List<Type> registeredSubscribers = new List<Type>();

        /// <summary>
        /// ctor
        /// </summary>
        internal BusConfiguration(IContainer container = null) {
            MaxThreads = 1;
            TopicName = "pro_ext_topic";
            this.container = container;
        }

        /// <summary>
        /// The Service Bus
        /// </summary>
        public IBus Bus {
            get {
                return container.Resolve<IBus>();
            }
        }

        /// <summary>
        /// The IOC Container
        /// </summary>
        public static IContainer Container {
            get {
                return configuration.container;
            }
        }

        /// <summary>
        /// DefaultSerializer
        /// </summary>
        public IServiceBusSerializer DefaultSerializer {
            get {
                return container.Resolve<IServiceBusSerializer>();
            }
        }

        /// <summary>
        /// Instance of BusConfiguration
        /// </summary>
        public static BusConfiguration Instance {
            get {
                //TODO should this throw an exception if it was not created?
                return configuration;
            }
        }

        /// <summary>
        /// Max Threads to call the message handlers from the bus messages being received
        /// </summary>
        public byte MaxThreads {
            get;
            internal set;
        }

        /// <summary>
        /// List of RegisteredAssemblies
        /// </summary>
        public IList<Assembly> RegisteredAssemblies {
            get {
                return this.registeredAssemblies;
            }
        }

        /// <summary>
        /// List of RegisteredSubscribers
        /// </summary>
        public IList<Type> RegisteredSubscribers {
            get {
                return this.registeredSubscribers;
            }
        }

        /// <summary>
        /// ServiceBusApplicationId
        /// </summary>
        public string ServiceBusApplicationId {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusNamespace
        /// </summary>
        public string ServiceBusNamespace {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusIssuerName
        /// </summary>
        public string ServiceBusIssuerName {
            get;
            internal set;
        }

        /// <summary>
        /// ServiceBusIssuerKey
        /// </summary>
        public string ServiceBusIssuerKey {
            get;
            internal set;
        }

        /// <summary>
        /// ServicePath
        /// </summary>
        public string ServicePath {
            get;
            internal set;
        }

        /// <summary>
        /// TopicName
        /// </summary>
        public string TopicName {
            get;
            internal set;
        }

        internal void Configure(ContainerBuilder builder) {
            Guard.ArgumentNotNull(builder, "builder");
            if (string.IsNullOrWhiteSpace(ServiceBusApplicationId)) {
                throw new ApplicationException("ApplicationId must be set.");
            }

            builder.Register<AzureBus>(item => new AzureBus(this)).As<IBus>().SingleInstance();

            builder.RegisterType<AzureBusReceiver>().As<IAzureBusReceiver>().SingleInstance();
            builder.RegisterType<AzureBusSender>().As<IAzureBusSender>().SingleInstance();

            if (container == null) {
                container = builder.Build();
            }
            else {
                builder.Update(container);
            }

            //Set the Bus property so that the receiver will register the end points
            var prime = this.Bus;
        }

        /// <summary>
        /// Get the settings builder optionally passing in your existing IOC Container
        /// </summary>
        /// <param name="container">Your optional existing IOC container.</param>
        /// <returns></returns>
        public static BusConfigurationBuilder WithSettings() {
            return WithSettings(null);
        }

        /// <summary>
        /// Get the settings builder optionally passing in your existing IOC Container
        /// </summary>
        /// <param name="container">Your optional existing IOC container. Use <see cref="WithSettings()"/> if you do not have an existing container.</param>
        /// <returns></returns>
        public static BusConfigurationBuilder WithSettings(IContainer container) {
            if (configuration == null) {
                lock (lockObject) {
                    if (configuration == null) {
                        configuration = new BusConfiguration(container);
                    }
                }
            }
            var builder = new ContainerBuilder();
            //last one in wins so if one is registered it will be called.
            builder.RegisterType<JsonServiceBusSerializer>().As<IServiceBusSerializer>().SingleInstance();
            return new BusConfigurationBuilder(builder, configuration);
        }

        internal void AddRegisteredAssembly(Assembly value) {
            Guard.ArgumentNotNull(value, "value");
            if (!this.registeredAssemblies.Contains(value)) {
                this.registeredAssemblies.Add(value);
            }
        }

        internal void AddRegisteredSubscriber(Type value) {
{'fraction_non_alphanumeric': 0.09907145740815503, 'fraction_numerical': 0.00016148566814695197, 'mean_word_length': 1.5475113122171946, 'pattern_counts': {'":': 0, '<': 121, '<?xml version=': 0, '>': 123, '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': '1369571', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2782, 'n_words': 733}