2

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!

Landin Martens
  • 3,283
  • 12
  • 43
  • 61

2 Answers2

9

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();
    }
}
Joe Daley
  • 45,356
  • 15
  • 65
  • 64
  • When I have the code above and then do trustedRemoteLoadDomain.Load("Path/To/File/Or/URL"); I get bad code base exception. If I copy the assembly so it is right beside the one running and do Assembly.Load it works perfectly. Why? – Landin Martens Aug 25 '12 at 14:04
  • Copying an assembly locally allows it to be loaded with full trust. If that is an option, then you don't need loadFromRemoteSource anyway. Perhaps edit your question to explain what you are doing that requires loadFromRemoteSource to be enabled? – Joe Daley Aug 26 '12 at 03:34
  • It was just a test, when the application is live it won't have a local access to the DLL. – Landin Martens Aug 26 '12 at 04:18
  • After testing and a bit of a modification to fit my code it worked great, just amazing! Thank you so much for your effect Joe!!!! – Landin Martens Aug 26 '12 at 06:30
  • This might not apply to the OP´s situation, but in my case with a unittesting scenario this worked for me : http://b2ben.blogspot.dk/2014/07/running-visual-studio-tests-from.html – noontz Dec 13 '17 at 12:52
-1

For my opinion the best way to solve the problem is to edit app.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <runtime>
    <loadFromRemoteSources enabled="true"/>
  </runtime>
</configuration>

you have to add

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

and the problem is solved. This solution is documented in MSDN

GMG
  • 1,498
  • 14
  • 20