views:

210

answers:

1

UPDATED!

I've highlighted the line that throws the error and added the code-behind for one of my User Controls (its at the bottom).


I have the following method which allows me to dynamically add "user controls" via code behind.
Like this:

UserControl myCtrl = LoadControl( "/pathtocontrol/control.ascx", p1, p2 );
litControl.AddControl( myCtrl );

The LoadControl method:

public static UserControl LoadControl(string UserControlPath, params object[] constructorParameters)
{
    var p = new System.Web.UI.Page();
    var ctl = p.LoadControl(UserControlPath) as System.Web.UI.UserControl;

    if (ctl != null)
    {
        // Find the relevant constructor
        System.Reflection.ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constructorParameters.Select(constParam => constParam == null ? "".GetType() : constParam.GetType()).ToArray());

        //And then call the relevant constructor
        if (constructor == null)
        {
            throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
        }

        /***************************************************/
        /***************************************************/
        /******** THE ERROR IS THROWN AT THIS LINE! ********/
        constructor.Invoke(ctl, constructorParameters);
        /***************************************************/
        /***************************************************/

    }

    // Finally return the fully initialized UC
    return ctl;
}

Which when executed on a Godaddy Shared Hosting account (IIS 7.0) gives me System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Is there anyway to make this method work on a GoDaddy shared host?


Here is the code behind for one of my user controls:

using System;
using DM;

public partial class controls_CustomerSummaryLine : System.Web.UI.UserControl
{
    public Customer Customer;
    public bool Highlight;
    public void Page_Load(object sender, EventArgs e)
    {

    }

    public controls_CustomerSummaryLine()
    {

    }
    public controls_CustomerSummaryLine(Customer customer)
    {
        Customer = customer;
    }
    public controls_CustomerSummaryLine(Customer customer, bool highlight)
    {
        Customer = customer;
        Highlight = highlight;
    }
}
+2  A: 

You are on a shared host and are most likely running in medium trust.

As such, ReflectionPermission is likely restricted.

You should contact your host and ask for guidance, but I do not have high hopes.

Update:

As I suspected, ConstructorInfo.Invoke involvement with ReflectionPermission is extensive and the documentation is not entirely clear, but what you should take away from this is that your current trust level is what is preventing your code from running without exceptions.

A real simple reality check here is: Does the code run ok locally? Yes? Does the code crash with a security exception on the shared host? Yes?

My nose says you have trust issues.

from http://msdn.microsoft.com/en-us/library/x7xy3xtx(VS.90).aspx

Sky Sanders
From the article you posted: "Without ReflectionPermission, code can use reflection to access only the public members of objects." My code calls a public constructor (e.g., `public controls_ItemSummaryLine(){}`). So how could this be a problem?
David Murdoch
@David - your code appears to be capable of doing more than that, but that is neither here nor there. It would be helpful if you could isolate the line that throws the error.
Sky Sanders
I've updated the question to provide more info. Thanks
David Murdoch
@David - answer is updated but still the same.
Sky Sanders
Thanks. But...I don't have trust issues - GoDaddy does! ( get it? )
David Murdoch
also, do you know of any other way to dynamically load my user controls WITH arguments?
David Murdoch
@david - not off the top of my head. I haven't done anything like that in years. Sorry. I am sure you will figure something out. Good luck with that and be sure to update your question with the solution when/if you find it. I would be interested in seeing how it pans out.
Sky Sanders
Update: Unfortunately "Enable strong naming on precompiled assemblies" and "AllowPartiallyTrustedCallerAttribute" didn't change the error. GoDaddy basically just said "No, it can't be done".
David Murdoch