如何创建应用程序域并在其中运行我的应用程序?

发布于 2024-08-29 05:33:02 字数 270 浏览 5 评论 0原文

我需要创建一个自定义应用程序域来解决 .NET 运行时 默认行为。我在网上看到的示例代码都没有帮助,因为我不知道将其放置在哪里,也不知道它需要在我的 Main() 方法中替换什么。

I need to create a custom application 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-09-05 05:33:02

可能应该注意的是,创建 AppDomains 只是为了绕过可以用常量字符串修复的东西可能是错误的方法。如果您尝试执行与您指出的链接相同的操作,则可以这样做:

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

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!");

Recursive Entry Point :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, args);

        Environment.ExitCode = ret;
        return;
    }

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

Quick sample using a nonstatic secondary entry point and 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);
    }
}
小红帽 2024-09-05 05:33:02

您需要:

1) 创建 AppDomainSetup 对象的实例,并使用您想要的域设置信息填充它

2) 使用 AppDomain.CreateDoman 方法创建新域。带有配置参数的 AppDomainSetup 实例被传递给 CreateDomain 方法。

3) 使用域对象上的 CreateInstanceAndUnwrap 方法在新域中创建对象的实例。此方法采用您要创建的对象的类型名,并返回一个远程代理,您可以在您的主域中使用它来与新域中创建的对象进行通信。

完成这 3 个步骤后,您可以通过以下方式调用其他域中的方法:代理。您还可以在完成后卸载域并再次重新加载。

MSDN 帮助中的这个主题提供了您需要的非常详细的示例

You need to:

1) Create an instance of AppDomainSetup object and populate it with the setup information you want for your domain

2) Create your new domain by using AppDomain.CreateDoman method. The AppDomainSetup instance with configuration parameters is passed to the CreateDomain method.

3) Create an instance of your object in the new domain by using the CreateInstanceAndUnwrap method on the domain object. This method takes typename of the object you want to create and returns a remoting proxy you can use in yuor main domain to communicate with the object created in the new one

Once you are through with these 3 steps you can call methods in the other domain through the proxy. You can also unload the domain after you are done and reload it again.

This topic in MSDN help has pretty detailed example of what you need

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文