WCF and ASP.NET - Server.Execute throwing object reference not set to an instance of an object

Posted by user208662 on Stack Overflow See other posts from Stack Overflow or by user208662
Published on 2010-03-29T18:38:30Z Indexed on 2010/03/29 18:43 UTC
Read the original article Hit count: 335

Filed under:
|

Hello,

I have an ASP.NET page that calls to a WCF service. This WCF service uses a BackgroundWorker to asynchronously create an ASP.NET page on my server. Oddly, when I execute the

WCF Service

[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());
    }
  }
}

Oddly, when the currentContext.Server.Execute method is executed, it throws an "object reference not set to an instance of an object" error. The reason this is so strange is because I can look at the currentContext properties in the watch window. In addition, Server is not null. Because of this, I have no idea where this error is coming from.

Can someone point me in the correct direction of what the cause of this could be?

Thank you!

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about wcf