C# Configure LoadFromRemoteSources through code not app.config?

浪子不回头ぞ 提交于 2019-12-12 19:22:16

问题


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!


回答1:


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();
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/12119548/c-sharp-configure-loadfromremotesources-through-code-not-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!