【发布时间】:2010-03-29 18:38:30
【问题描述】:
我有一个调用 WCF 服务的 ASP.NET 页面。此 WCF 服务使用 BackgroundWorker 在我的服务器上异步创建 ASP.NET 页面。奇怪的是,当我执行
WCF 服务
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void PostRequest(string comments)
{
// Do stuff
// If everything went o.k. asynchronously render a page on the server. I do not want to
// block the caller while this is occurring.
BackgroundWorker myWorker = new BackgroundWorker();
myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerAsync(HttpContext.Current);
}
private void myWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Set the current context so we can render the page via Server.Execute
HttpContext context = (HttpContext)(e.Argument);
HttpContext.Current = context;
// Retrieve the url to the page
string applicationPath = context.Request.ApplicationPath;
string sourceUrl = applicationPath + "/log.aspx";
string targetDirectory = currentContext.Server.MapPath("/logs/");
// Execute the other page and load its contents
using (StringWriter stringWriter = new StringWriter())
{
// Write the contents out to the target url
// NOTE: THIS IS WHERE MY ERROR OCCURS
currentContext.Server.Execute(sourceUrl, stringWriter);
// Prepare to write out the result of the log
targetPath = targetDirectory + "/" + DateTime.Now.ToShortDateString() + ".aspx";
using (StreamWriter streamWriter = new StreamWriter(targetPath, false))
{
// Write out the content to the file
sb.Append(stringWriter.ToString());
streamWriter.Write(sb.ToString());
}
}
}
奇怪的是,在执行 currentContext.Server.Execute 方法时,它会抛出“对象引用未设置为对象的实例”错误。之所以如此奇怪,是因为我可以在监视窗口中查看 currentContext 属性。此外,Server 不为空。因此,我不知道这个错误是从哪里来的。
有人能指出造成这种情况的正确方向吗?
谢谢!
【问题讨论】:
-
“这个 WCF 服务使用一个 BackgroundWorker 在我的服务器上异步创建一个 ASP.NET 页面”——我的脑袋都炸了。