跨AppDomain的调用是在调用方域执行 [英] Cross AppDomain call is executed in caller Domain

查看:174
本文介绍了跨AppDomain的调用是在调用方域执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个AppDomain,创建新的域对象的实例并调用返回的包装对象对当前的AppDomain的名称的方法。返回的值是主程序域的名称,而不是新创建的之一。顺便说一下code正在执行一个的UnitTest在VS2010。

任何想法,为什么测试失败?

  [Serializable接口]
    公共类DomainHelper
    {
        公共字符串GetDomainName()
        {
            返回AppDomain.CurrentDomain.FriendlyName;
        }
    }


    [识别TestClass]
    公共类DomainTests
    {
        [测试方法]
        公共无效为RemoteCall()
        {
            VAR BINDIR = Path.GetDirectoryName(新的URI(Assembly.GetExecutingAssembly()codeBase类).LocalPath。);

            常量字符串appDomainName =TEST;
            VAR X = AppDomain.CreateDomain(appDomainName,空,BINDIR,空,假);

            VAR remoteType = typeof运算(DomainHelper);
            VAR远程=(DomainHelper)x.CreateInstanceAndUnwrap(remoteType.Assembly.FullName,remoteType.FullName);

            Assert.AreEqual(appDomainName,remote.GetDomainName());
        }
    }
 

解决方案

由于该 DomainHelper [Serializable接口] 。这意味着,当它穿过域,它被复制并重新创建在调用者的域,事后 .GetDomainName 在调用方的域执行。您可以删除 [Serializable接口] 属性,并有 DomainHelper MarshalByRefObject的,那么 .GetDomainName 将在远程域执行,或者保持 [Serializable接口] 属性和检索域名在构造函数或初始化程序,像这样:

  [Serializable接口]
公共类DomainHelper
{
    私人只读字符串_domainIWasConstructedIn = AppDomain.CurrentDomain.FriendlyName;

    公共字符串GetDomainName()
    {
        返回_domainIWasConstructedIn;
    }
}
 

在初始化/构造函数然后在远程域对象时,跨域执行,并设置相关领域将被复制。

I create an AppDomain, create an instance of an object in the new Domain and call a method that returns the name of the current AppDomain on the wrapped object. the returned value is the name of the main program domain and not the newly created one. By the way the code is being executed as a UnitTest in VS2010.

Any Idea why the test fails?

[Serializable]
    public class DomainHelper
    {
        public string GetDomainName()
        {
            return AppDomain.CurrentDomain.FriendlyName;
        }
    }


    [TestClass]
    public class DomainTests
    {
        [TestMethod]
        public void RemoteCall()
        {
            var binDir = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);

            const string appDomainName = "TEST";
            var x = AppDomain.CreateDomain(appDomainName, null, binDir,null, false);

            var remoteType = typeof(DomainHelper);
            var remote = (DomainHelper) x.CreateInstanceAndUnwrap(remoteType.Assembly.FullName, remoteType.FullName);

            Assert.AreEqual(appDomainName, remote.GetDomainName());
        }
    }

解决方案

Because the DomainHelper is [Serializable]. Which means when it crosses domains, it is copied and recreated in the caller's domain, and afterwards .GetDomainName is executed in the caller's domain. You can either remove the [Serializable] attribute and have the DomainHelper derive from MarshalByRefObject, then the .GetDomainName would be executed in the remote domain, or keep the [Serializable] attribute and retrieve the domain name in a constructor or initializer, like so:

[Serializable]
public class DomainHelper
{
    private readonly string _domainIWasConstructedIn = AppDomain.CurrentDomain.FriendlyName;

    public string GetDomainName()
    {
        return _domainIWasConstructedIn;
    }
}

The initializer/constructor would then execute in the remote domain, and the relevant fields it sets would be copied when the object crosses domains.

这篇关于跨AppDomain的调用是在调用方域执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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