Re: Assembly loaded from URL needs to read registry - but can't

From: gkelly (gkelly_at_xmission.com)
Date: 02/28/05


Date: Sun, 27 Feb 2005 23:19:54 -0700

For those of you that are interested. Here is a solution that works well.
However, the solution given by
Shawn Farkas should be viewed by all. It's very informative.

   System.Security.Policy.Evidence v_ev = new
System.Security.Policy.Evidence();
   v_ev.AddHost( new Site( "<host>" ));
   v_ev.AddHost( new Zone(SecurityZone.MyComputer ));

   Assembly v_assembly = null;

   string v_dllhost = http://>/;

   string[] v_dlls =
   {
    "dll1.dll",
    "dll2.dll",
    "main.dll",
   };

   foreach( string v_dll in v_dlls )
   {
    v_assembly = Assembly.LoadFrom( v_dllhost + v_dll, v_ev );
   }

   Type v_objtype = v_assembly.GetType( "fba.frmMain" );
   try
   {
    BindingFlags v_bf = BindingFlags.Instance | BindingFlags.Static |
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic;

    MethodInfo mi = v_objtype.GetMethod("Main", v_bf );
    mi.Invoke( null, null);
   }
   catch( Exception ex )
   {
    int a = 1;
   }

...................

I will use a variation of the above code to allow multile entry points.
There are several "apps" I want to be able to run

"gkelly" <gkelly@xmission.com> wrote in message
news:OVUPsPtGFHA.3612@TK2MSFTNGP09.phx.gbl...
>I have a set of applications that use a common assembly library.
>
> AppA, AppB and AppC all use LibA and LibB
>
> These will run on about 25 computers.
>
> AppA and LibA need to read registry to determine database connect string
> to use and other misc config stuff
>
> For a while anyway, all code will be updated periodically. So, I decided
> place code on our web server.
>
> Instead of AppA and AppB being "Windows Applications" I changed them to
> Class Libraries.
>
> I created a Launcher App that will use Assembly.LoadFrom to load from a
> url like
http://server/applib/AppA.dll
>
> However, since code loaded this way runs in a different context I get an
> error error such as:
>
> Request for the permission of type
> System.Security.Permissions.RegistryPermission failed..
>
> I have read about this and somewhat understand the issues involved.
>
> Question: Is there a "simple" way to load a dll from a URL and have it run
> with the same permission as code that was
> loaded locally?
>
> I thought the following would work, but it doesnt. Same error happens.
> So, obviously my understanding is very limited.
>
> Evidence ev = new Evidence();
> ev.Merge( AppDomain.CurrentDomain.Evidence );
> ev.AddHost( new Zone( SecurityZone.MyComputer ));
> Assembly v_assembly = Assembly.LoadFrom( "http://server/applib/AppA.dll",
> ev );
>
> when creating an object instance from this assembly it still can't read
> the registry. I "thought" this would load the assembly so that's
> it's code would run with same permissions as the launcher. It doesn't.
>
> Any help would be appreciated.
>
> gkelly
>
>