调用另一个应用程序域 [英] Calling into another app domain

查看:59
本文介绍了调用另一个应用程序域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有以下问题.

我的C#dll加载到ID为id2的AppDomain中.该dll还具有一个"cominterface"类,可为COM导出某些方法.

My C# dll is loaded in AppDomain with id id2. The dll also has a class 'cominterface' which exports some method for COM.

当在AppDomain中执行ID为id1(!= id2)的导出的COM方法之一被调用时.

When one of the exported COM Method ere called the are executed in AppDomain with id id1 (!= id2).

但是在这种方法中,我必须调用其他类(相同dll)的其他方法.在ID为id2的AppDomain中.

But in this method I must call other method of other classes (of the same dll)  in the AppDomain with id id2.

我该如何实现?

最诚挚的问候

  亨德里克

    Hendrik

 

 

推荐答案

我写了一个扩展方法来做到这一点.出于我的目的,我需要在不同的应用程序域中运行代码,以便可以使用不同的app.config文件.我最初写此代码是为了增强我的单元测试框架,这样我就不必创建一个新的单元 测试项目,以测试我的应用程序可用的每个可能的app.config选项.

I wrote an extension method to do this.  For my purposes, I need to run code in different app domains so I can use different app.config files.  I wrote this initially to augment my unit testing framework so that I didn't have to create a new unit test project for to test every possible app.config option available to my application.

相关的部分是使用MarshalByRefType的位.

The relevant part is the bit which uses a MarshalByRefType.

请注意,尝试传递无法序列化的参数时会遇到问题.您正在跨越应用程序边界,因此未共享内存.

Note that you will run into issues trying to pass parameters that aren't serializable.  You're crossing an application boundary and as such memory is not being shared.

 public static void ExecuteInAppDomain(this Action action, string domainName = null, string configurationFile = null)

  {

   domainName = domainName ?? Guid.NewGuid().ToString();

   var ads = new AppDomainSetup();

   ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;   

   ads.DisallowBindingRedirects = false;

   if (string.IsNullOrWhiteSpace(configurationFile) == false)

   {

    if (Path.IsPathRooted(configurationFile) == false)

    {

     var baseFile = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

     var directory = baseFile.Directory;

     var newFile = directory.GetFile(configurationFile);

     if (newFile.Exists)

     {

      configurationFile = newFile.FullName;

     }

     else

     {

      throw new ArgumentException("Configuration file " + configurationFile + " does not exist.");

     }

    }

    ads.ConfigurationFile = configurationFile;

   }

   else

   {

    ads.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

   }

   var baseEvidence = AppDomain.CurrentDomain.Evidence;

   var evidence = new Evidence(baseEvidence);

   var domain = AppDomain.CreateDomain(domainName, evidence, ads);

   

   try

   {

    var mbrtType = typeof(MarshalByRefType);

    MarshalByRefType mbrt = (MarshalByRefType)domain.CreateInstanceAndUnwrap(mbrtType.Assembly.FullName,

     mbrtType.FullName);

    mbrt.Do(action);

   }

   finally

   {

    AppDomain.Unload(domain);

   }

  }




这篇关于调用另一个应用程序域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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