locked
Operation could destabilize the runtime RRS feed

  • Question

  • User-2011861869 posted
    Hello

    I have a custom web control and get this error when I run under Medium Trust:

    InnerException: Operation could destabilize the runtime.
    Message: DotNetNuke.Services.Exceptions.PageLoadException: Operation could destabilize the runtime. ---> System.Security.VerificationException: Operation could destabilize the runtime. at SmartThinker.DNN.Modules.ModuleFramework.UI.SmartTimePicker.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

    Can anyone shed any light on it? I need to run in Medium Trust and I am not doing anything fancy in the render method of the control:

            protected override void Render(HtmlTextWriter writer)
            {
                //layout table for control       
                writer.AddAttribute("Cellspacing", "0");
                writer.AddAttribute("Cellpadding", "0");
                writer.AddAttribute("Border", "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Table); //table
                writer.RenderBeginTag(HtmlTextWriterTag.Tr); //tr

                //hrs combo       
                writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                hrsDD.RenderControl(writer);
                writer.RenderEndTag(); //td
               
                //add hrs label
                writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                lblHrs.RenderControl(writer);
                writer.RenderEndTag(); //td

                //Add a spacer
                writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                new LiteralControl(" ").RenderControl(writer);
                writer.RenderEndTag(); //td

                //min combo
                writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                minsDD.RenderControl(writer);
                writer.RenderEndTag(); //td

                //add mins label
                writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
                lblMins.RenderControl(writer);
                writer.RenderEndTag(); //td

                writer.RenderEndTag(); //tr
                writer.RenderEndTag(); //table
            }


    Much appreciated - I have spent ages searching for a solution to this!


    Wednesday, April 12, 2006 9:14 AM

All replies

  • User331136620 posted

    We saw this back towards the end of Whidbey for a case where 1.1 DLLs were being used in an ASP.NET 2.0 app.  Apparently the 1.1 compilers for C#/VB.NET had some boundary cases with the IL they emitted.  In 2.0 there were low-level changes in the CLR that made some 1.1 code unverifiable - which leads to the exception you are seeing.  Unfortunately I can't claim to understand what it was the CLR changed - I just know we saw it.

    In our specific case it was a problem with a C# switch statement that had more than 8 conditions - underneath the hood the C# compiler in 1.1 emitted IL code that became unverifiable in 2.0.  I don't see any switch statements in your code above so that's not going to be the specific problem.

    To track down the problem, you should try the old fallback of chopping out all the code except the first line.  Then run the app - see if it fails.  Then uncomment the second line code code - run the app - see if it fails.  Rinse and repeat.  That's literally how we tracked down the issues on our team.

    Wednesday, April 12, 2006 5:02 PM
  • User-2011861869 posted
    Thanks for the reply Stefan - I think you're on to something here. The subcontrol has a large switch statement:


                switch (listType)
                {
                    case ListType.User:
                        UserController userController = new UserController();
                        listDataSource = userController.GetUsers(Utilities.GetModuleUserControl(this).PortalId, true, true);
                        dataValueField = "UserID";
                        dataTextField = "FullName";
                        break;
                    case ListType.ApprovalStatus:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_ApprovalStatus");
                        break;
                    case ListType.CensorStatus:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_Censorship");
                        break;
                    case ListType.GeneralStatus:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_GeneralStatus");
                        break;
                    case ListType.EventCategory:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventCategory");
                        break;
                    case ListType.EventDressCode:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventDressCode");
                        break;
                    case ListType.EventDuration:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventDuration");
                        break;
                    case ListType.EventStatus:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventStatus");
                        break;
                    case ListType.EventType:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventType");
                        break;
                    case ListType.EventRSVP:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventRSVP");
                        break;
                    case ListType.Title:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_Title");
                        break;
                    case ListType.EventInviteType:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventInviteType");
                        break;
                    case ListType.QuoteCategory:
                        listDataSource = listController.GetListEntryInfoCollection("SmartThinker_QuoteCategory");
                        break;
                }

    it's late, so I'll only be able to test this in a while, but I'll follow your advice.

    Wednesday, April 12, 2006 8:03 PM
  • User331136620 posted

    Heh - looks exactly like what was in our 1.1 assembly - a big switch statement.  Our hack was to change the 1.1 code over to a bunch of if-then statements.  Then we recompiled with VS2003, and were able to successfully use the updated assembly in a partial trust ASP.NET 2.0 app.

    Wednesday, April 12, 2006 9:37 PM
  • User-2011861869 posted
    *sigh* - that didn't work Stefan ;)

    I tried a big list of  "if - else if " stamements with no joy, and then straight "if statements", but no joy... (still same error)

    Any other ideas please?


                if (listType == ListType.User)
                {
                    UserController userController = new UserController();
                    listDataSource = userController.GetUsers(Utilities.GetModuleUserControl(this).PortalId, true, true);
                    dataValueField = "UserID";
                    dataTextField = "FullName";
                }
                if (listType == ListType.ApprovalStatus)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_ApprovalStatus");
                }
                if (listType == ListType.CensorStatus)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_Censorship");
                }
                if (listType == ListType.GeneralStatus)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_GeneralStatus");
                }
                if (listType == ListType.EventCategory)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventCategory");
                }
                if (listType == ListType.EventDressCode)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventDressCode");
                }
                if (listType == ListType.EventDuration)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventDuration");
                }
                if (listType == ListType.EventStatus)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventStatus");
                }
                if (listType == ListType.EventType)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventType");
                }

                if (listType == ListType.EventRSVP)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventRSVP");
                }
                if (listType == ListType.Title)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_Title");
                }
                if (listType == ListType.EventInviteType)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_EventInviteType");
                }
                if (listType == ListType.QuoteCategory)
                {
                    listDataSource = listController.GetListEntryInfoCollection("SmartThinker_QuoteCategory");
                }

    Thursday, April 13, 2006 8:20 AM
  • User-2011861869 posted
    Very odd - I've narrowed it down to creating and rendering a LiteralControl in the render method of the custom control - this line:

    new LiteralControl(" ").RenderControl(writer);

    I don't know enough to know why - is that what you would expect?

    Thursday, April 13, 2006 9:48 AM
  • User331136620 posted

    Is this custom control an ASP.NET 1.1 control?  Just wondering because the line of code above works fine in a page on ASP.NET 2.0 running under Medium trust.

    If you remove the line of code you have above, does the custom control work then?

    I don't know why the line of code above would cause the problem - I don't understand the ins and outs of what makes IL verifiable versus not verifiable.  As a long shot - what happens if you first construct the LiteralControl on one line - and then on the next line call RenderControl(..)?

    Thursday, April 13, 2006 4:58 PM
  • User-2011861869 posted
    Yes, it's a 1.1 control - if  I comment it out it works fine... very strange - I will try your suggestion and get back to you - thanks...
    Friday, April 14, 2006 2:16 PM
  • User-2011861869 posted
    Interesting:

    LiteralControl spacer = new LiteralControl(" ");
    spacer.RenderControl(writer);

    seems to work... so it's the

    new LiteralControl(" ").RenderControl(writer);

    that breaks it!?
    Friday, April 14, 2006 2:46 PM
  • User331136620 posted
    Beats me why that fixes it!  I'll ask some CLR folks internally if they have any ideas on why rearranging the code in this case fixes the problem.
    Friday, April 14, 2006 3:03 PM
  • User331136620 posted

    Turns out there was a low-level change to how the 2.0 Framework verifies code for a specific type of IL.  And in some cases the older 1.1 compilers emit IL that is now considered unverifiable in 2.0.  This was the response from the CLR gurus:

    The Everett compiler emits the following code for the original 1.1 call:

      IL_0011:  ldloc.0
      IL_0012:  call       instance void [System.Web]System.Web.UI.Control::RenderControl(class [System.Web]System.Web.UI.HtmlTextWriter)

    However RenderControl is a virtual method and due to the stricter requirements for ASP.NET 2.0, this is not considered a safe way to call a virtual method in Whidbey.

    The code change that you made gets emitted as:

      IL_0013:  ldloc.0
      IL_0014:  callvirt   instance void [System.Web]System.Web.UI.Control::RenderControl(class [System.Web]System.Web.UI.HtmlTextWriter)

    From a verification stanpoint, "callvirt" is safe - but "call" isn't.

    Friday, April 14, 2006 6:15 PM
  • User-2011861869 posted
    Hmm - interesting - thanks for the help - hopefully this post will  help some other people who run into this obscure problem!

    Saturday, April 15, 2006 5:02 AM
  • User10602474 posted
    For the last 3 hours I am struggling to solve this "Operation could destabilize the runtime" error. This I encountered when I switched over from Asp.net 1.1 to Asp.net 2.0. After reading the post, I checked my code and changed the SWITCH statement to if else if and now things are running fine.

    Thanks a lot to all.
    Wednesday, June 7, 2006 4:45 PM
  • User-1040200577 posted

    I have the same error when I want to Render a dataGrid.

      dataGrid.RenderControl(oHtmlTextWriter);

     For the problems mentioned in this thread, a tric did the job. I cannot find the tric for the dataGrid.

    Thanks in advance... I have searched and struggled for days now, but nothing helps... do I have to wait for Delphi 200x, which fully support ASP.NET 2.0, or is there a tric for a dataGrid.Render...

     Regards,

     Peter

    Sunday, October 15, 2006 9:15 AM
  • User331136620 posted
    If you have a sample page with code that reproduces the problem, go ahead and post the sample here.  We can take a look at it.
    Monday, October 16, 2006 2:31 PM
  • User-1040200577 posted

    Thank you for your reply, I hope you can help.

    Here is my sample code. The method dataGrid1.RenderControl(oHtmlTextWriter) is working fine under asp.net1.1 but the 'famous' error occurs under asp.net 2.0. Thank you very much in advance...

    procedure tTest.Page_Load(sender: System.Object; e: System.EventArgs);
    begin
      fillgrid;
    end;

    procedure tTest.FillGrid;
    var
      dt : datatable;
      dr : datarow;
      i  : integer;
      dc : datacolumn;
      s  : string;
      r  : double;

    begin
       dt:= datatable.Create;
       dc:= datacolumn.Create('Column1');
       dt.Columns.Add(dc);
       dc:= datacolumn.Create('Column2');
       dt.Columns.Add(dc);
       dc:= datacolumn.Create('Column3');
       dt.Columns.Add(dc);
       for i:=1 to 10 do
       begin
         dr:=dt.NewRow;
         dr[0] := i.ToString;
         dr[1] := 'Item ' + i.ToString();
         r:=1.23 * (i + 1);
         str(r:0:2,s);
         dr[2]:=s;
         dt.Rows.add(dr)
       end;
       dv := dataview.Create(dt);
       dataGrid1.DataSource:=dv;
       dataGrid1.DataBind;
    end;

    procedure TTest.Button1_Click(sender: System.Object; e: System.EventArgs);
    var
      i : integer;
      oHtmlTextWriter: System.Web.UI.HtmlTextWriter;
      oStringWriter : System.IO.StringWriter;
    begin
      Response.Clear;
      Response.Buffer:=true;
      Response.ContentType:= 'application/vnd.ms-excel';
      Response.AddHeader('Content-Disposition', 'attachment; filename=test.xls');
      Response.ContentEncoding:= System.Text.Encoding.UTF7;
      Response.Charset := '';
      oStringWriter:=System.IO.StringWriter.Create;
      oHtmlTextWriter:=System.Web.UI.HtmlTextWriter.Create(oStringWriter);

      dataGrid1.RenderControl(oHtmlTextWriter);
      Response.Write(oStringWriter.ToString());
      Response.&End;
    end;
     

    Saturday, October 21, 2006 5:29 AM
  • User331136620 posted

    I manually converted this into C#.  The C# version runs without any errors, which leads me to think that the Delphi compiler is emitting IL that the .NET 2.0 Framework doesn't like.  Just for reference I have pasted the converted version below:

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillGrid();
        }

        private void FillGrid()
        {
            DataTable dt = new DataTable();
            DataRow dr;
            double r;
           
            dt.Columns.Add(new DataColumn("Column1"));
            dt.Columns.Add(new DataColumn("Column2"));
            dt.Columns.Add(new DataColumn("Column3"));

            for (int i =0; i<10; i++)
            {
                dr = dt.NewRow();
                dr[0] = i.ToString();
                dr[1] = "Item " + i.ToString();
                r = 1.23 * (i + 1);  
                dr[2] =r.ToString(); //shortcut - this doesn't preserve the original formatting
                dt.Rows.Add(dr);
            }

            DataView dv = new DataView(dt);
     
            dataGrid1.DataSource =dv;
            dataGrid1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int i;
            System.Web.UI.HtmlTextWriter oHtmlTextWriter;
            System.IO.StringWriter oStringWriter;

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType ="'application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.Charset = "";

            oStringWriter = new System.IO.StringWriter();
            oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            dataGrid1.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();

        }
    }

    Monday, October 23, 2006 8:21 PM