Exemple #1
0
        /// <summary>
        /// Gets the user name
        /// </summary>
        /// <returns>return the user name</returns>
        private static string GetUserName()
        {
#if HPCPACK
            // case 1. This handles aad integration.
            if (Thread.CurrentPrincipal.IsHpcAadPrincipal(WinServiceHpcContextModule.GetOrAddWinServiceHpcContextFromEnv()))
            {
                return(Thread.CurrentPrincipal.Identity.Name);
            }

            // case 2. OperationContext.Current == null.  This handles inproc broker
            if (OperationContext.Current == null)
            {
                // Returns the log on user of the current thread if OperationContext.Current is null (apply to inprocess broker)
                return(WindowsIdentity.GetCurrent().Name);
            }
#endif

            // Stand alone mode goes here
            if (OperationContext.Current == null)
            {
                return(Constant.AnonymousUserName);
            }

            // case 3. OperationContext.Current.ServiceSecurityContext == null.  This handles insecure binding
            // Note: SOA REST service always use secure binding.
            if (OperationContext.Current.ServiceSecurityContext == null)
            {
                return(Constant.AnonymousUserName);
            }

            // case 4.  OperationContext.Current.ServiceSecurityContext != null and caller is not SOA REST service.
            if (OperationContext.Current.ServiceSecurityContext.IsAnonymous)
            {
                return(Constant.AnonymousUserName);
            }

            return(OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name);
        }
Exemple #2
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">indicating arguments</param>
        private static int Main(string[] args)
        {
            var log = new LoggerConfiguration().ReadFrom.AppSettings().Enrich.WithMachineName().CreateLogger();

            Log.Logger = log;

            if (!ParseAndSetBrokerWorkerSettings(args))
            {
                // parsing failed
                return((int)BrokerShimExitCode.ForceExit);
            }

            if (ConfigureLogging)
            {
                Trace.TraceInformation("Log configuration for Session Launcher has done successfully.");
                Log.CloseAndFlush();
                return((int)BrokerShimExitCode.ForceExit);
            }

            //SingletonRegistry.Initialize(SingletonRegistry.RegistryMode.WindowsNonHA);
#if HPCPACK
            WinServiceHpcContextModule.GetOrAddWinServiceHpcContextFromEnv().GetAADClientAppIdAsync().FireAndForget(); // cache AAD AppId now.
#endif
            // improve http performance for Azure storage queue traffic
            ServicePointManager.DefaultConnectionLimit = 1000;
            ServicePointManager.Expect100Continue      = false;
            ServicePointManager.UseNagleAlgorithm      = false;

            int pid = Process.GetCurrentProcess().Id;

            trace = TraceHelper.RuntimeTrace;

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            ManualResetEvent exitWaitHandle = new ManualResetEvent(false);

            // ThreadPoolMonitor.StartOnlyInDebug();

            try
            {
                Uri brokerManagementServiceAddress = new Uri(SoaHelper.GetBrokerManagementServiceAddress(pid));

                ServiceHost host;

                try
                {
                    BrokerManagementService instance = new BrokerManagementService(exitWaitHandle);

                    trace.LogBrokerWorkerMessage(pid,
                                                 string.Format("[Main] Try open broker management service at {0}.", brokerManagementServiceAddress.ToString()));

                    host = new ServiceHost(instance, brokerManagementServiceAddress);
                    host.CloseTimeout = TimeSpan.FromSeconds(1);
                    host.AddServiceEndpoint(typeof(IBrokerManagementService), BindingHelper.HardCodedBrokerManagementServiceBinding, String.Empty);
                    host.Open();

                    trace.LogBrokerWorkerMessage(pid, "[Main] Open broker management service succeeded.");
                }
                catch (Exception e)
                {
                    trace.LogBrokerWorkerUnexpectedlyExit(pid,
                                                          string.Format("[Main] Failed to open broker management service: {0}", e));

                    return((int)BrokerShimExitCode.FailedOpenServiceHost);
                }

                bool            createdNew;
                EventWaitHandle initializeWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset, String.Format(Constant.InitializationWaitHandleNameFormat, pid), out createdNew);
                if (createdNew && !BrokerWorkerSetting.Default.Debug)
                {
                    trace.LogBrokerWorkerUnexpectedlyExit(pid,
                                                          "[Main] Initialize wait handle has not been created by the broker launcher.");

                    return((int)BrokerShimExitCode.InitializeWaitHandleNotExist);
                }

                if (!initializeWaitHandle.Set())
                {
                    trace.LogBrokerWorkerUnexpectedlyExit(pid,
                                                          "[Main] Failed to set the initialize wait handle.");

                    return((int)BrokerShimExitCode.FailedToSetInitializeWaitHandle);
                }

                // Wait for exit
                exitWaitHandle.WaitOne();

                try
                {
                    // Make sure server is terminated correctly and the client is getting notified
                    // Swallow exception and ignore any failure because we don't want broker to retry
                    host.Close();
                }
                catch (Exception ex1)
                {
                    trace.LogBrokerWorkerMessage(
                        pid,
                        string.Format(CultureInfo.InvariantCulture, "[Program].Main: Exception {0}", ex1));

                    trace.LogBrokerWorkerMessage(pid,
                                                 "[Main] Failed to close the ServiceHost.");

                    try
                    {
                        host.Abort();
                    }
                    catch (Exception ex)
                    {
                        trace.LogBrokerWorkerMessage(
                            pid,
                            string.Format(CultureInfo.InvariantCulture, "[Program].Main: Exception {0}", ex));

                        trace.LogBrokerWorkerMessage(pid,
                                                     "[Main] Failed to abort the ServiceHost.");
                    }
                }

                return((int)BrokerShimExitCode.Success);
            }
            catch (Exception e)
            {
                trace.LogBrokerWorkerUnexpectedlyExit(pid, String.Format("[Main] Exception thrown to ROOT: {0}", e));
                throw;
            }
            finally
            {
                trace.LogBrokerWorkerMessage(pid, "[Main] Process exit.");
                Log.CloseAndFlush();
            }
        }