C# Configure LoadFromRemoteSources through code not app.config?

Landin Martens picture Landin Martens · Aug 25, 2012 · Viewed 10.3k times · Source

I currently have an app.config with the following config value:

<configuration>
 <runtime>
  <loadFromRemoteSources enabled="true" />
 </runtime>
</configuration>

So far I have been able to remove everything else out of my app.config and configure things just using pure C# code except the loadFromRemoteSources.

Is there a way to enable loadFromRemoteSources through C# code?

I understand the pros and cons of having an app.config, but I would prefer not to have one.

Thanks!

Answer

Joe Daley picture Joe Daley · Aug 25, 2012

I can't find any way to enable loadFromRemoteSources in code, and if there is, I doubt it would allow you to apply it to an application domain after it has been created. But I could be wrong!

You could instead use the technique described in "More Implicit Uses of CAS Policy: loadFromRemoteSources". They suggest creating a new application domain with PermissionState.Unrestricted, and loading remote assemblies into there. An application domain created this way does not throw NotSupportedException when you try to load a remote assembly.

They describe this technique as a solution for when you want to load only some remote assemblies with full trust, and thus do not want to enable loadFromRemoteSources in your app.config. You could use the same technique, but load your entire program into the new app domain.

Here is an example program which, when executed, immediately creates a new application domain with PermissionState.Unrestricted and runs itself in it:

public class Program : MarshalByRefObject
{
    public Program()
    {
        Console.WriteLine("Program is running.");
        Assembly remoteAssembly = Assembly.LoadFrom(
            @"\\server\MyRemoteAssembly.dll");
        IAddIn addIn = (IAddIn)remoteAssembly.CreateInstance("MyAddIn");
        addIn.Initialize();
    }

    static void Main()
    {
        // This program needs to run in an application domain with
        // PermissionState.Unrestricted, so that remote assemblies can be loaded
        // with full trust. Instantiate Program in a new application domain.
        PermissionSet permissionSet =
            new PermissionSet(PermissionState.Unrestricted);
        AppDomainSetup setup = new AppDomainSetup();
        setup.ApplicationBase =
            AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        AppDomain appDomain = AppDomain.CreateDomain(
            "Trusted Domain", null, setup, permissionSet);
        appDomain.CreateInstance(
            Assembly.GetExecutingAssembly().FullName, "Program");
    }
}

public interface IAddIn
{
    void Initialize();
}

And here is the source code for MyRemoteAssembly.dll, which you can put on your network share:

public class MyAddIn : IAddIn
{
    public void Initialize()
    {
        Console.WriteLine("Hello from a remote assembly!");
        Console.ReadLine();
    }
}