Note that there are some explanatory texts on larger screens.

plurals
  1. POAccess appSettings in web.config from Thread in Class Library project
    text
    copied!<p>Can I able to access <code>appSettings</code> section in my ASP.NET web.config file from a method in another referenced Class Library project when it is called as a new <code>Thread</code>? I'm accessing the setting through a property as</p> <pre><code> private static string TempXmlFolder { get { return System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML"); } } </code></pre> <p>There is an extension method for the matter to generate the receipt.</p> <pre><code> 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(); } } </code></pre> <p>I'm calling the receipt generation as a new thread from the class library like</p> <pre><code> 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(); </code></pre> <p>But since the <code>HttpContext.Current</code> is null inside the <code>Thread</code>, I'm not able to access the current web server configuration file.</p> <p>Can you suggest there any way other than passing the current <code>HttpContext</code> to the <code>Thread</code>? If no, what are the things I've to take care to keep thread safety?</p> <h2>Edit #1</h2> <p>Currently I'm passing the HttpContext to the thread like</p> <pre><code> System.Web.HttpContext currentContext = System.Web.HttpContext.Current; Thread printThread = new Thread(() =&gt; this.GenerateReceipt(currentContext)); </code></pre> <p>and in the function,</p> <pre><code> internal static void GenerateReceipt(this IMatter matter, System.Web.HttpContext htCont) { string TempXmlFolder = htCont.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML"); //... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload