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

开发 后端
平常我们在做服务开发的时候,经常是希望本地可以直接调试;在生产环境是以服务允许的;这时候,一般的做法写2段代码,需要什么环境就注释那段代码,这样很麻烦,这时候就可以利用Environment判断当前的环境。

[[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.   } 

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2020-10-30 10:49:37

DockerGPULinux

2020-10-19 09:50:04

Spinnaker

2013-07-29 14:23:55

Eclipse开发环境Eclipse开发环境

2019-08-08 08:00:00

深度学习机器学习神经网络

2013-10-16 09:38:23

云端应用部署工具应用部署工具云测试

2020-12-08 12:59:42

编译烧录 for Mac

2012-02-20 15:04:58

2013-09-30 09:40:37

VDI部署VDI

2021-03-10 07:20:42

Redis命令数据

2011-06-17 16:47:12

Qt Eclipse Windows

2011-06-17 17:16:01

Qt Eclipse Linux

2011-06-17 17:01:22

Qt Eclipse Windows

2024-02-01 09:06:44

Company​Employee场景

2022-05-26 09:00:00

网站抓取Lightrun开发

2018-12-04 14:15:29

容器误区开发

2015-02-27 09:00:54

Docker微服务容器技术

2020-11-10 08:00:00

机器学习技术科学

2012-02-29 14:35:48

MySQL

2010-09-01 16:43:26

Squid ACLSquid访问列表Squid

2018-02-11 09:00:00

软件部署监控
点赞
收藏

51CTO技术栈公众号