gpt4 book ai didi

.net - 如何在线程或定时器中访问 HttpServerUtility.MapPath 方法?

转载 作者:行者123 更新时间:2023-12-03 05:03:02 24 4
gpt4 key购买 nike

我在我的 Asp.Net 应用程序中使用 System.Timers.Timer ,并且我需要使用 HttpServerUtility.MapPath 方法,该方法似乎只能通过 使用>HttpContext.Current.Server.MapPath。问题在于,当 Timer.Elapsed 事件触发时,HttpContext.Currentnull

还有其他方法来获取 HttpServerUtility 对象的引用吗?我可以将它注入(inject)到我的类的构造函数中。安全吗?我如何确定当前请求结束时不会被垃圾收集?

谢谢!

最佳答案

可以使用HostingEnvironment.MapPath()代替HttpContext.Current.Server.MapPath()

不过,我还没有在线程或计时器事件中尝试过。

<小时/>

我考虑过的一些(不可行的)解决方案;

  • 我在 HttpServerUtility 上唯一关心的方法是 MapPath。因此,作为替代方案,我可以使用 AppDomain.CurrentDomain.BaseDirectory 并从中构建我的路径。 但是,如果您的应用使用虚拟目录,这将会失败(我的应用就是如此)。

  • 另一种方法:将我需要的所有路径添加到 Global 类中。在 Application_Start 中解析这些路径。

关于.net - 如何在线程或定时器中访问 HttpServerUtility.MapPath 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/111927/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com