C#通过代码而不是app.config配置LoadFromRemoteSources? [英] C# Configure LoadFromRemoteSources through code not app.config?

查看:861
本文介绍了C#通过代码而不是app.config配置LoadFromRemoteSources?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个app.config具有以下配置值:

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

到目前为止,我已经能够从我的app.config中删除一切,是否有一种方法可以启用 loadFromRemoteSources

$



通过C#代码?



我理解有一个app.config的优点和缺点,但我不想有一个。



谢谢!

解决方案

我找不到任何方式启用 loadFromRemoteSources 在代码中,如果有,我怀疑它会允许你应用它到一个应用程序域,在它被创建后。但我可能错了!



您可以改用 CAS隐私政策使用:loadFromRemoteSources 。他们建议使用 PermissionState.Unrestricted 创建一个新的应用程序域,并将远程程序集加载到其中。以此方式创建的应用程序域在尝试加载远程程序集时不会引发 NotSupportedException



技术作为您希望仅加载完全信任的远程程序集的一些解决方案,因此不想在您的应用程序中启用 loadFromRemoteSources 。配置。您可以使用相同的方法,但将整个程序加载到新的应用程序域。



这是一个示例程序,立即使用 PermissionState.Unrestricted 创建一个新的应用程序域,并在其中运行自身:

  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()
{
//此程序需要在
// PermissionState.Unrestricted的应用程序域中运行,因此远程组件可以加载
//完全信任。在新的应用程序域中实例化程序。
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();
}

这里是 MyRemoteAssembly.dll的源代码,您可以将其放在您的网络共享上:

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


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!

解决方案

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

这篇关于C#通过代码而不是app.config配置LoadFromRemoteSources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆