netcore检测启动模式是否调试模式的两种实现模式

20 篇文章 0 订阅

1.使用条件编译符#if Debug,当代码被此标签包含时,换到Release调试模式,代码不会被编译,且访问Controller/Action 会返回404

#if DEBUG
    /// <summary>
    /// 测试
    /// </summary>
    [AllowAnonymous]
    public class TestController : BaseController
    {
        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [EnableDebug]
        [EnableHostEnvionment("Development")]
        public FuncResult<Test> GetSensitiveData(string data)
        {
            return new Test() { SensitiveData = data };
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class Test
    {
        /// <summary>
        /// 
        /// </summary>
        [SensitiveData]
        public string SensitiveData { get; set; }
    }

#endif

2.使用特性过滤器:特性打在指定Action或者Controller上,内部判断当前是否调试模式返回的数据是自定义格式

引处参考:https://blog.csdn.net/WPwalter/article/details/80933080

    // <summary>
    /// 调试模式:Debug,Release
    /// </summary>
    public class EnableDebugAttribute : ActionFilterAttribute
    {
        private bool isDebug;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isDebug"></param>
        public EnableDebugAttribute(bool isDebug=true)
        {
            this.isDebug = isDebug;
        }

        /// <summary>
        /// Action开始之前执行
        /// </summary>
        /// <param name="actionContext"></param>
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            //应用程序启动debug/release模式
            var isDebugCurrent = DebuggingProperties.IsDebug;
            if (isDebug != isDebugCurrent)
            {
                actionContext.Result = new JsonResult(FuncResult.Fail("不允许的启动模式(当前:" + (isDebugCurrent ? "Debug" : "Release") + ")"));
            }
            base.OnActionExecuting(actionContext);
        }

        /// <summary>
        /// 包含在运行时判断编译器编译配置中调试信息相关的属性。
        /// </summary>
        public static class DebuggingProperties
        {
            /// <summary>
            /// 检查当前正在运行的主程序是否是在 Debug 配置下编译生成的。
            /// </summary>
            public static bool IsDebug
            {
                get
                {
                    if (_isDebugMode == null)
                    {
                        var assembly = Assembly.GetEntryAssembly();
                        if (assembly == null)
                        {
                            // 由于调用 GetFrames 的 StackTrace 实例没有跳过任何帧,所以 GetFrames() 一定不为 null。
                            assembly = new StackTrace().GetFrames().Last().GetMethod().Module.Assembly;
                        }

                        var debuggableAttribute = assembly.GetCustomAttribute<DebuggableAttribute>();
                        _isDebugMode = debuggableAttribute.DebuggingFlags.HasFlag(DebuggableAttribute.DebuggingModes.EnableEditAndContinue);
                    }

                    return _isDebugMode.Value;
                }
            }

            private static bool? _isDebugMode;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netcore 可以通过使用网络通信技术实现聊天功能。一种常见的方式是使用Socket套接字进行通信。 首先,我们需要一个服务器端和多个客户端。服务器端负责接收和转发消息,而客户端用于发送和接收消息。 服务器端的实现可以使用Netcore的TcpListener类。该类允许我们创建一个TCP服务器,监听指定的端口,等待客户端的连接。一旦有客户端连接,服务器通过TcpClient接收和发送消息。 客户端的实现可以使用Netcore的TcpClient类。该类允许我们连接到服务器端,并通过网络流发送和接收消息。 在聊天应用中,客户端可以输入消息并发送给服务器,服务器收到消息后转发给所有连接的客户端。客户端也能接收服务器和其他客户端发送的消息,并将其显示在用户界面上。 为了实现实时聊天,我们可以通过在服务器和客户端之间建立一个持久连接来实现。这意味着连接会一直保持打开状态,以便服务器和客户端可以实时发送和接收消息。 使用Netcore的异步编程模型可以提高聊天应用的性能和可扩展性。可以使用async和await关键字来处理异步任务。通过使用异步套接字操作,我们可以同时处理多个连接和消息,避免阻塞或挂起主线程。 总而言之,Netcore提供了丰富的网络编程功能,可以方便地实现聊天应用。通过使用Sockets和异步编程模型,我们可以构建一个高性能、实时的聊天系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值