WCF服务中的RenderControl失败; ASMX有效

时间:2009-07-29 20:32:55

标签: asp.net wcf dynamic content-management-system

下午好!我花了半天时间试图解决这个问题;希望你们中的一个可以提供帮助。

我编写了一个有限的CMS,正在努力允许CMS中的内容在外部网站中显示为插页式页面。为此,我想创建一个WCF服务,该服务将呈现相应的内容并将其作为字符串返回到客户端页面中。

但是,我遇到了一种奇怪的行为。 RadioButtonListCheckBoxList控件最终在NullReferenceException期间抛出RenderControlInternal(显然不是我的贡献)。所有其他控件(文本框,列表框,下拉列表,自定义复合控件)都可以正常呈现。

我在WCF服务旁边创建了一个ASMX服务,并直接从WCF服务剪切/粘贴了代码。它完美地呈现了控件。

为什么WCF服务有选择地导致炸弹的任何线索?

以下是服务方法代码:

    public string RenderPageContents(int pageId)
    {
        string output = string.Empty;

        SiteBuilder.Page p = new SiteBuilder.Page();
        if (p.Load(pageId))
        {
            foreach (SiteBuilder.PageItem pi in p.Items)
            {
                if (pi.LoadItemDetails(true))
                {
                    // Calls LoadControl from \Bin
                    Web.Controls.Base.BuilderControlBase bcb =
                          Web.Controls.ControlHelper.CreateSiteBuilderControl(
                              @"C:\Development\SitebuilderService\", 
                             pi.ItemDetails);

                    if (bcb != null)
                    {
                        bcb.Configure(pi);
                        output += ControlHelper.RenderControl(ref bcb);
                    }
                }
            }
        }

        return output;
    }

以下是ControlHelper.RenderControl的代码(是的,它在VB中。它存在于另一个项目中)。

Public Shared Function RenderControl(ByRef ctrl As Controls.Base.BuilderControlBase) _
      As String
    Dim sb As New StringBuilder()
    Dim tw As New StringWriter(sb)
    Dim hw As New HtmlTextWriter(tw)

    ctrl.RenderControl(hw)

    Return sb.ToString()
End Function

异常消息是“对象引用未设置为对象的实例”。

这是使用WCF时抛出的Exception的堆栈跟踪:

at System.Web.UI.WebControls.RadioButtonList.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at System.Web.UI.WebControls.Table.RenderContents(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
at System.Web.UI.WebControls.TableCell.RenderContents(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) 
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.RenderContents(HtmlTextWriter writer) 
at System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) 
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) 
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) 
at XXX.ControlHelper.RenderControl(BuilderControlBase& ctrl) 
    in C:\TFS\XXX\Development\XXX\Web\Controls\Builder\Helpers\ControlHelper.vb:line 81 
at XXX.Services.SiteBuilderService.RenderPageContents(Int32 pageId) 
    in C:\TFS\Development\XXX.Web.Services\XXX.Web.Services\SiteBuilderService.svc.cs:line 61

  

单独调用时,WCF服务是否返回正确的字符串?

我不确定我理解你的确切含义。什么分开?如果我调用从测试工具进行处理的业务类,是的,它可以正常工作。当我从ASMX服务中调用它时,它可以正常工作。从WCF,radiobutton渲染中断。

  

您对WCF服务使用什么样的绑定和安全性?

我正在使用basicHttpBinding。

<binding name="BasicHttpBinding_ISiteBuilderService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="1024000" maxBufferPoolSize="524288" maxReceivedMessageSize="1024000"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="1024000" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None"
            realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
    </binding>


<endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISiteBuilderService" contract="SiteBuilder.ISiteBuilderService" name="BasicHttpBinding_ISiteBuilderService" />

<service behaviorConfiguration="Xxx.SiteBuilderServiceBehavior"
   name="Xxx.Services.SiteBuilderService">
    <endpoint address="" binding="basicHttpBinding" contract="Kaplan.OEC.Services.ISiteBuilderService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>

<behavior name="Xxx.Services.SiteBuilderServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  

如果您在例如SoapUI - 它是否以预期的格式返回字符串?或者某些标签和/或特殊字符可能会被破坏?

从SoapUI它也无法正确呈现,出现与WCF相同的错误。

2 个答案:

答案 0 :(得分:2)

您是否尝试在WCF配置中启用ASP.NET compatibility mode?该错误可能与HttpContext不可用有关(尽管如果我可以这么说,你当前的方法虽然可行,但是相当丑陋: - )

答案 1 :(得分:0)

单独调用时,WCF服务是否返回正确的字符串?您为WCF服务使用什么样的绑定和安全性?你可以发布一些配置(服务器端,可能还有客户端)?

最接近ASMX遗留服务的绑定将是basicHttpBinding,基本上所有安全措施都已关闭。你在用它吗?

如果您在例如SoapUI - 它是否以预期的格式返回字符串?或者某些标签和/或特殊字符可能会被破坏?