WCF and ASP.NET - Server.Execute throwing object reference not set to an instance of an object
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.R开发者_JS百科equest.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!
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
- MSDN: hosting WCF services side-by-side with ASP.NET
- WCF ASP.NET Compatibility Mode
for very extensive coverage of all the details.
精彩评论