简体   繁体   English

从类库项目中的线程访问web.config中的appSettings

[英]Access appSettings in web.config from Thread in Class Library project

Can I able to access appSettings section in my ASP.NET web.config file from a method in another referenced Class Library project when it is called as a new Thread ? 将另一个新类Thread调用时,是否可以从另一个引用的类库项目中的方法访问ASP.NET web.config文件中的appSettings部分? I'm accessing the setting through a property as 我正在通过属性访问设置

 private static string TempXmlFolder
 {
      get
      {
           return System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML");
      }
 }

There is an extension method for the matter to generate the receipt. 有一种扩展方法可以用于生成收据。

 internal static void GenerateReceipt(this IMatter matter)
 {
     try
     {
          string XmlFile = TempXmlFolder + "/Rec_" + matter.MatterID + ".xml";
          // ...
          // Generating receipt from the matter contents
          // ...
          // Saving generated receipt
     }
     catch (Exception ex)
     {
          ex.WriteLog();
     }
 }

I'm calling the receipt generation as a new thread from the class library like 我将收据生成称为类库中的新线程,例如

 Thread printThread = new Thread(new ThreadStart(this.GenerateReceipt));
 // To avoid exception 'The calling thread must be STA, because many UI components require this' (Using WPF controls in receipt generation function)
 printThread.SetApartmentState(ApartmentState.STA);
 printThread.Start();
 // ...
 // Do another stuffs
 // ...
 // Wait to generate receipt to complete
 printThread.Join();

But since the HttpContext.Current is null inside the Thread , I'm not able to access the current web server configuration file. 但是由于ThreadHttpContext.Current为null,因此我无法访问当前的Web服务器配置文件。

Can you suggest there any way other than passing the current HttpContext to the Thread ? 除了将当前的HttpContext传递给Thread您是否可以建议其他方法? If no, what are the things I've to take care to keep thread safety? 如果没有,为保证线程安全我必须注意些什么?

Edit #1 编辑#1

Currently I'm passing the HttpContext to the thread like 目前,我正在将HttpContext传递给像

 System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
 Thread printThread = new Thread(() => this.GenerateReceipt(currentContext));

and in the function, 在功能上

 internal static void GenerateReceipt(this IMatter matter, System.Web.HttpContext htCont)
 {
      string TempXmlFolder = htCont.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML");
      //...

Pass the TempXmlFolder into the thread. TempXmlFolder传递到线程中。 Don't rely on HttpContext.Current . 不要依赖HttpContext.Current Alternatively, pass the value of HttpContext.Current to the thread and calculate the value of TempXmlFolder later. 或者,将HttpContext.Current的值传递给线程,然后再计算TempXmlFolder的值。

You can pass the value using any way you want. 您可以使用任意方式传递值。 Maybe a field or a local variable that you capture with a lambda. 可能是您使用lambda捕获的字段或局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM