我可以访问吗
appSettings
在另一个引用的类库项目中的方法中的ASP.NET web.config文件中的节作为新方法调用时
Thread
?
我正在通过属性访问设置,作为
private static string TempXmlFolder
{
get
{
return System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML");
}
}
这件事有一种扩展的方法来生成收据。
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();
}
}
我把收据生成称为类库中的一个新线程,就像
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();
但自从
HttpContext.Current
在内部为null
线
,我无法访问当前的web服务器配置文件。
除了通过电流,你能建议其他方法吗
HttpContext
到
线
? 如果没有,我必须注意哪些事情才能保证线程安全?
编辑#1
目前我正在将HttpContext传递给线程
System.Web.HttpContext currentContext = System.Web.HttpContext.Current;
Thread printThread = new Thread(() => this.GenerateReceipt(currentContext));
并且在功能中,
internal static void GenerateReceipt(this IMatter matter, System.Web.HttpContext htCont)
{
string TempXmlFolder = htCont.Server.MapPath(ConfigurationManager.AppSettings["ReceiptTempPath"] ?? "~/Receipts/TempXML");
//...