巧用Environment.UserInteractive 实现开发和生产环境的分开调试部署

[[411578]]

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。

概述

平常我们在做服务开发的时候,经常是希望本地可以直接调试;在生产环境是以服务允许的;这时候,一般的做法写2段代码,需要什么环境就注释那段代码,这样很麻烦,这时候就可以利用Environment判断当前的环境。C#中获取系统环境变量需要用到Environment 类。其中提供了有关当前环境和平台的信息以及操作它们的方法。该类不能被继承,以下代码得到%systemdrive%的值,即“C:”

 
 
 
  1. string sPath = Environment.GetEnvironmentVariable("systemdrive"); 
  2. Console.WriteLine(sPath); 

获取一个值,用以指示当前进程是否在用户交互模式中运行。

 
 
 
  1. public static bool UserInteractive { get; } 

如果当前进程在用户交互模式中运行,则为true ;否则为 false。

注解

此UserInteractive 属性报告 false 的 Windows 进程或服务(如 IIS)在没有用户界面的情况下运行。如果此属性为 false ,则不会显示模式对话框或消息框,因为没有可供用户与之交互的图形用户界面。

Program范例

 
 
 
  1. internal static class Program 
  2.   { 
  3.       /// <summary> 
  4.       /// 應用程式的主要進入點。 
  5.       /// </summary> 
  6.       private static void Main(string[] args) 
  7.       { 
  8.           args = new string[1]; 
  9.           args[0] = "WeChat.SendTemplateMsgJob"
  10.  
  11.           bool isReleaseUpdateJob = Environment.UserInteractive // 上線更新舊資料,都只會手動執行一次 
  12.               && args.Length >= 1 
  13.               && args[0].StartsWith("ReleaseUpdate"); 
  14.           //Autofac 
  15.           AutofacConfig.Bootstrapper(); 
  16.  
  17.           if (Environment.UserInteractive) 
  18.           { 
  19.               if (args.Length == 0) 
  20.               { 
  21.                   //Console 開啟 
  22.                   MainService mainService = new MainService(); 
  23.                   mainService.TestStartAndStop(args); 
  24.               } 
  25.               else 
  26.               { 
  27.                   //指定想要測試的 job 
  28.  
  29.                   #region set Culture en-US 
  30.  
  31.                   Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); 
  32.                   Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US"); 
  33.  
  34.                   #endregion set Culture en-US 
  35.  
  36.                   if (isReleaseUpdateJob) 
  37.                   { 
  38.                       string jobType = $"BigCRM.WinService.Jobs.{args[0]}"
  39.                       ReleaseUpdateJob job = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, jobType).Unwrap() as ReleaseUpdateJob; 
  40.                       job.Call(null, args); 
  41.                   } 
  42.                   else 
  43.                   { 
  44.                       #region load config 
  45.  
  46.                       List<JobConfigItem> jobConfigItems = JobConfigItem.Get(); 
  47.                       JobConfigItem config = jobConfigItems.FirstOrDefault(m => m.JobType == args[0]); 
  48.  
  49.                       #endregion load config 
  50.  
  51.                       #region init job 
  52.  
  53.                       string jobType = $"BigCRM.WinService.Jobs.{config.JobType}"
  54.                       BaseJob job = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, jobType).Unwrap() as BaseJob; 
  55.                       job.CronSchedule = config.CronExpression; 
  56.                       job.JobType = config.JobType; 
  57.                       job.BaseSettings = config.Settings; 
  58.                       if (config.Settings != null
  59.                       { 
  60.                           job.Settings = new Quartz.JobDataMap(config.Settings); 
  61.                       } 
  62.  
  63.                       #endregion init job 
  64.  
  65.                       job.Call(null, args); 
  66.                   } 
  67.  
  68.                   Console.ReadLine(); 
  69.               } 
  70.           } 
  71.           else 
  72.           { 
  73.               ServiceBase[] ServicesToRun; 
  74.               ServicesToRun = new ServiceBase[] 
  75.               { 
  76.                   new MainService() 
  77.               }; 
  78.               ServiceBase.Run(ServicesToRun); 
  79.           } 
  80.       } 
  81.   } 

 

THE END