如何创建一个AppDomain,并在其上​​运行我的应用程序 [英] How do I create an appDomain and run my application in it

查看:163
本文介绍了如何创建一个AppDomain,并在其上​​运行我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个自定义的应用程序域来解决的错误在.NET运行时的<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/420f51a1-2cc7-4e9b-9a7a-dcb4d3e5d0ca?prof=required&ppud=4\">default行为。样品code,我在网上看到的都不是有帮助的,因为我不知道在哪里放置它,或者它需要什么内更换我的的Main()方法。

I need to create a custom app domain to work around a bug in the .net runtime's default behavior. None of the sample code I've seen online is helpful since I don't know where to place it, or what it needs to replace within my Main() method.

推荐答案

这也许应该指出的是,创建的AppDomain只是为了让周围的东西,可以固定一个常量字符串可能是错误的方式做到这一点。如果你正在尝试做同样的事情链接你指出,你可能只是这样做...

It should probably be noted that creating AppDomains just to get around something that can be fixed with a constant string is probably the wrong way to do it. If you are trying to do the same thing as the link you noted you could just do this...

var configFile = Assembly.GetExecutingAssembly().Location + ".config";
if (!File.Exists(configFile))
    throw new Exception("do your worst!");

递归入口点:O)

    static void Main(string[] args)
    {
        if (AppDomain.CurrentDomain.IsDefaultAppDomain())
        {
            Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);

            var currentAssembly = Assembly.GetExecutingAssembly();
            var otherDomain = AppDomain.CreateDomain("other domain");
            var ret = otherDomain.ExecuteAssemblyByName(
                                        currentAssembly.FullName, 
                                        currentAssembly.Evidence,
                                        args);
            Environment.ExitCode = ret;
            return;
        }

        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
        Console.WriteLine("Hello");
    }

使用非静态辅助入口点和MarshalByRefObject的快速采样...

class Program
{
    static AppDomain otherDomain;

    static void Main(string[] args)
    {
        otherDomain = AppDomain.CreateDomain("other domain");

        var otherType = typeof(OtherProgram);
        var obj = otherDomain.CreateInstanceAndUnwrap(
                                 otherType.Assembly.FullName,
                                 otherType.FullName) as OtherProgram;

        args = new[] { "hello", "world" };
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
        obj.Main(args);
    }
}
public class OtherProgram : MarshalByRefObject
{
    public void Main(string[] args)
    {
        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
        foreach (var item in args)
            Console.WriteLine(item);
    }
}

这篇关于如何创建一个AppDomain,并在其上​​运行我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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