示例#1
0
        public override bool OnStart()
        {
            // Turn down the verbosity of traces written by Azure
            RoleEnvironment.TraceSource.Switch.Level = SourceLevels.Information;

            // Set the maximum number of concurrent connections
            ServicePointManager.DefaultConnectionLimit = 12;

            // Bootstrap Serilog logger
            // Bootstrap serilog logging
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Information().WriteTo.Trace()
                         .CreateLogger();

            Log.Information("KillrVideo.BackgroundWorker is starting");

            try
            {
                // Initialize the Windsor container
                _windsorContainer = WindsorBootstrapper.CreateContainer();

                // Create the logical worker instances and fire async OnStart
                _logicalWorkers = new ILogicalWorkerRole[]
                {
                    new VideoCatalog.Worker.WorkerRole(_windsorContainer),
                    new Search.Worker.WorkerRole(_windsorContainer),
                    new Uploads.Worker.WorkerRole(_windsorContainer),
                    new SampleData.Worker.WorkerRole(_windsorContainer)
                };

                Task[] startTasks = _logicalWorkers.Select(w => Task.Run(() => w.OnStart())).ToArray();

                // Wait for all workers to start
                Task.WaitAll(startTasks);

                return(base.OnStart());
            }
            catch (AggregateException ae)
            {
                foreach (var exception in ae.Flatten().InnerExceptions)
                {
                    Log.Fatal(exception, "Unexpected exception while starting background worker");
                }

                throw new Exception("Background worker failed to start", ae);
            }
            catch (Exception e)
            {
                Log.Fatal(e, "Unexpected exception while starting background worker");
                throw;
            }
        }
        public override bool OnStart()
        {
            // Turn down the verbosity of traces written by Azure
            RoleEnvironment.TraceSource.Switch.Level = SourceLevels.Information;

            // Set the maximum number of concurrent connections
            ServicePointManager.DefaultConnectionLimit = 12;

            // Bootstrap Log4net logging and serilog logger
            XmlConfigurator.Configure();
            _logger    = LogManager.GetLogger(typeof(WorkerRole));
            Log.Logger = new LoggerConfiguration().WriteTo.Log4Net().CreateLogger();

            _logger.Info("KillrVideo.BackgroundWorker is starting");

            try
            {
                // Initialize the Windsor container
                _windsorContainer = WindsorBootstrapper.CreateContainer();

                // Create the logical worker instances
                var logicalWorkers = new ILogicalWorkerRole[]
                {
                    new VideoCatalog.Worker.WorkerRole(_windsorContainer),
                    new Search.Worker.WorkerRole(_windsorContainer),
                    new Uploads.Worker.WorkerRole(_windsorContainer),
                    new SampleData.Worker.WorkerRole(_windsorContainer)
                };

                // Fire OnStart on all the logical workers
                CancellationToken token = _cancellationTokenSource.Token;
                foreach (ILogicalWorkerRole worker in logicalWorkers)
                {
                    ILogicalWorkerRole worker1 = worker;
                    _logicalWorkerTasks.Add(Task.Run(() => worker1.OnStart(token), token));
                }

                return(base.OnStart());
            }
            catch (Exception e)
            {
                _logger.Error("Exception in BackgroundWorker OnStart", e);
                throw;
            }
        }