コード例 #1
0
ファイル: BaseEndpointConfig.cs プロジェクト: eraj2587/NSB7
        public virtual EndpointConfiguration BuildConfig()
        {
            LogManager.Use <DefaultFactory>().Level(LogLevel.Debug);

            if (string.IsNullOrEmpty(_configEndpointName))
            {
                throw new ArgumentNullException(_configEndpointName, "Endpoint name cannot be null");
            }

            var endpointConfiguration = new EndpointConfiguration(_configEndpointName);

            if (_isSendOnly)
            {
                endpointConfiguration.SendOnly();
            }

            // builder = GetContainerBuilder();
            //IEndpointInstance endpoint;

            // Variation on https://docs.particular.net/samples/dependency-injection/autofac/
            // builder.Register(x => Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult())
            //     .As<IEndpointInstance>()
            //     .SingleInstance();

            // _container = builder.Build();

            //var temp = _container.Resolve<IScheduledTask[]>();

            //  endpointConfiguration.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(_container));

            endpointConfiguration.EnableUniformSession();

            endpointConfiguration.SendFailedMessagesTo(GetErrorQueueName());
            endpointConfiguration.AuditProcessedMessagesTo(GetAuditQueueName());

            endpointConfiguration.UseSerialization <NewtonsoftSerializer>();
            // endpointConfiguration.HeartbeatPlugin(serviceControlQueue: "ServiceControl_Queue");

            //enable installers for DEV purposes
            if (GetInstallerFlag())
            {
                endpointConfiguration.EnableInstallers(); // TODO: Move to the appropriate place
            }
            //  else
            //     endpointConfiguration.DisableInstallers();

            //var assemblyScanner = endpointConfiguration.AssemblyScanner();
            //assemblyScanner.ThrowExceptions = false;

            var transportMode = GetTransportMode();

            if (transportMode == TransportMode.LocalFile)
            {
                var transport = endpointConfiguration.UseTransport <LearningTransport>();
                transport.StorageDirectory(GetLocalFileTransportModeLocation());
                BuildEndpointRouting(transport.Routing());
                endpointConfiguration.UsePersistence <LearningPersistence>();
            }
            else
            {
                var transport =
                    endpointConfiguration.UseTransport <SqlServerTransport>()
                    .ConnectionString(GetTransportConnectionString());

                transport.DefaultSchema(DefaultNsbSchema);
                BuildEndpointRouting(transport.Routing());

                var nhConfig = new NHibernate.Cfg.Configuration();
                nhConfig.SetProperty(Environment.ConnectionProvider, "NHibernate.Connection.DriverConnectionProvider");
                nhConfig.SetProperty(Environment.ConnectionDriver, "NHibernate.Driver.Sql2008ClientDriver");
                nhConfig.SetProperty(Environment.Dialect, "NHibernate.Dialect.MsSql2008Dialect");
                nhConfig.SetProperty(Environment.ConnectionString, GetPersistenceConnectionString());
                nhConfig.SetProperty(Environment.DefaultSchema, DefaultNsbSchema);
                endpointConfiguration.UsePersistence <NHibernatePersistence>()

#if !DEBUG
                .DisableSchemaUpdate()
#endif
                .UseConfiguration(nhConfig);
            }


            //Set message conventions
            endpointConfiguration.Conventions()
            .DefiningEventsAs(DefineEventTypes())
            .DefiningCommandsAs(DefineCommandTypes())
            .DefiningMessagesAs(DefineMessageTypes());

            //Set max concurrency;
            endpointConfiguration.LimitMessageProcessingConcurrencyTo(25);

            //Set retry
            var recoverability = endpointConfiguration.Recoverability();
            //immediate
            recoverability.Immediate(
                immediate =>
            {
                immediate.NumberOfRetries(3);
            });
            //delayed
            recoverability.Delayed(
                delayed =>
            {
                delayed.NumberOfRetries(2);
                delayed.TimeIncrease(TimeSpan.FromMinutes(3));
            });


            // Metrics does not apply top send only endpoints
            if (!_isSendOnly && GetMetricsFlag())
            {
                var performanceCounters = endpointConfiguration.EnableWindowsPerformanceCounters();
                performanceCounters.EnableSLAPerformanceCounters(TimeSpan.FromMinutes(GetMetricsSla()));
                //performanceCounters. UpdateCounterEvery(TimeSpan.FromSeconds(GetMetricsInterval()));

                var metrics = endpointConfiguration.EnableMetrics();

                metrics.SendMetricDataToServiceControl(
                    serviceControlMetricsAddress: "Particular.Monitoring",
                    interval: TimeSpan.FromSeconds(10));
            }

            // Activate Custom checks if enabled
            if (GetCustomCheckFlag())
            {
                var queue = GetCustomCheckQueueName();
                if (!string.IsNullOrEmpty(queue))
                {
                    endpointConfiguration.ReportCustomChecksTo(queue, TimeSpan.FromMinutes(GetCustomCheckTtlInMinutes()));
                }
            }

            // Activate saga plugin if enabled
            if (GetSagaPluginFlag())
            {
                endpointConfiguration.AuditSagaStateChanges(serviceControlQueue: "particular.servicecontrol@machine");
            }

            // Set up logging
            var logConfiguration = LogConfiguration.ReadFromConfigurationWithDefaultValues();
            logConfiguration.LogFileName = _configEndpointName;
            logConfiguration.EventSource = _configEndpointName;
            LogConfigurator.Configure(logConfiguration);

            // Set up audit logging
            LogConfigurator.ConfigureAudit(logConfiguration);

            // Set NSB to use NLog
            LogManager.Use <NLogFactory>();

            LocalCustomize(endpointConfiguration);

            return(endpointConfiguration);
        }