【问题标题】:WCF and ASP.NET - Server.Execute throwing object reference not set to an instance of an objectWCF 和 ASP.NET - Server.Execute 抛出对象引用未设置为对象的实例
【发布时间】: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 页面”——我的脑袋都炸了。

标签: asp.net wcf


【解决方案1】:

您正在使用HttpContext - 通常默认情况下可用于 WCF(并且将是 null) - 毕竟,WCF 可以在 IIS 之外自托管,并且ASP.NET 管道。

如果您需要并且想要使用 HttpContext,您需要明确允许并打开它。在您的服务器配置中,您需要:

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

你的服务类也应该用:

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

看看

非常广泛地涵盖所有细节。

【讨论】:

  • 奇怪的是,我只有在 BackgroundWorker 中运行代码时才会遇到问题。如果我执行阻塞调用,它工作正常。
  • @user208662:这一点也不奇怪,因为HttpContext.Current 只对实际处理请求的线程有效。
  • 有没有办法将 HttpContext 附加到 BackgroundWorker?我尝试将它传入。但我仍然得到如上所述的 null 异常。
  • 不,很可能不是 - 您真正需要 HttpContext 提供什么?你能以其他方式获得它并使其可供工作线程访问吗??
  • 我需要使用Server.Execute,它需要HttpContext。原因是因为我正在调用一个 .aspx 页面,而我正在渲染为一个 .txt 文件。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多