tags:

views:

154

answers:

1

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!

+1  A: 

You're using the HttpContext - that is typically not available to WCF by default (and will be null) - after all, WCF can be self-hosted outside of IIS and the ASP.NET pipeline.

If you need and want to use the HttpContext, you need to specifically allow it and turn it on. In your server's config, you need:

<system.serviceModel>        
    <serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />    
</system.serviceModel>

and your service class should also be decorated with:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class YourServiceImplementation : IYourService
......

Check out

for very extensive coverage of all the details.

marc_s
The oddity is, I only get the problem if I run my code in a BackgroundWorker. It works fine if I execute a blocking call.
@user208662: That's not odd at all, considering that `HttpContext.Current` is only valid for the thread that's actually serving the request.
Aaronaught
Is there any way to attach the HttpContext to the BackgroundWorker? I tried passing it in. But I still get the null exception as described above.
no, most likely not - what is it that you really need from the HttpContext? Can you get that some other way and make it accessible for the worker thread??
marc_s
I need to use Server.Execute, which requires the HttpContext. The reason why is because I'm calling an .aspx page that i'm rendering out to a .txt file.