当前位置: 首页 > news >正文

.net 公共变量 线程安全

项目场景:

比如一个仓储系统,可以切换仓房 ,每个仓房对接的设备链接不同 ,需要获取当前仓房的链接

这时候就需要一个公共量来表示这个链接,方便后续在不同的接口里调用,当多个用户操作这个系统时,需要保证这个公共量是当前用户操作的仓房链接


解决方案:

ConcurrentDictionary 这个可以有效保证线程安全

using Xhxx.Authorize;
using System.Configuration;
using System.Web.Http;
using Xhxx.Entity;
using ES.Web.Api;
using System.Net.Http;
using System.Web;
using System.Collections.Concurrent;namespace System.Linq
{
#if DEBUG[AllowAnonymous]
#endifpublic class NBBaseController : BaseController{private static readonly ConcurrentDictionary<string, string> WmsUrlDic = new ConcurrentDictionary<string, string>();
//当前仓发链接public  string CurrentWmsUrl{get {return WmsUrlDic.GetOrAdd(base.loginerBase.UserCode ?? "super","");}set {WmsUrlDic.AddOrUpdate(base.loginerBase.UserCode ?? "super", value, (k, v) => value);}}public NBBaseController(){}protected void GetCruentWmsUrl(string StoreHouseCode){lock (_locker){switch (StoreHouseCode){case "913302001440687412007":WmsUrlDic.TryUpdate(base.loginerBase.UserCode ?? "super", ConfigurationManager.AppSettings["WmsUrl"], CurrentWmsUrl);break;case "913302001440687412036":WmsUrlDic.TryUpdate(base.loginerBase.UserCode ?? "super", ConfigurationManager.AppSettings["WmsLkUrl"], CurrentWmsUrl);break;default:WmsUrlDic.TryUpdate(base.loginerBase.UserCode ?? "super", "", CurrentWmsUrl);break;}}}}}
}

http://www.dtcms.com/a/185366.html

相关文章:

  • flutter 的 json序列化和反序列化
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】金融风控分析案例-10.1 风险数据清洗与特征工程
  • 【技巧】离线安装docker镜像的方法
  • Python中操作Neo4j图数据库
  • LeetCode热题100——链表
  • IBM BAW(原BPM升级版)使用教程第十二讲
  • HTML17:表单初级验证
  • js应用opencv
  • Scala和Spark的介绍
  • 深入浅出 iOS 对象模型:isa 指针 与 Swift Metadata
  • Spring Boot 使用Itext绘制并导出PDF
  • 创建三个网络,分别使用RIP、OSPF、静态,并每个网络10个电脑。使用DHCP分配IP
  • C++ 中介者模式详解
  • SAM论文学习
  • Windows系统安装VirtualBox-7及其以上的版本修改默认安装路径后提示
  • python标准库--heapq - 堆队列算法(优先队列)在算法比赛的应用
  • 【AI News | 20250512】每日AI进展
  • 使用Daemonset部署日志收集守护进程
  • 探索边缘计算:赋能物联网的未来
  • WEBSTORM前端 —— 第3章:移动 Web —— 第1节:平面转换、渐变
  • 快消品商超业务单据解决方案重塑KA商超、电商业务与SAP ERP协同效率
  • 动态人脸识别教学实训沙盘功能介绍
  • 扩展:React 项目执行 yarn eject 后的 package.json 变化详解及参数解析
  • Linux进程10-有名管道概述、创建、读写操作、两个管道进程间通信、读写规律(只读、只写、读写区别)、设置阻塞/非阻塞
  • Spark处理过程-转换算子和行动算子
  • Lodash isEqual 方法源码实现分析
  • Spring Cloud Sleuth 链路追踪
  • Java面试高阶篇:Spring Boot+Quarkus+Redis高并发架构设计与性能优化实战
  • ZYNQ笔记(二十):Clocking Wizard 动态配置
  • 【开源工具】深度解析:基于PyQt6的Windows时间校时同步工具开发全攻略