IIS请求里面的AppDomain之间的双向通信 [英] Two-way communication between AppDomains inside of IIS Requests

查看:175
本文介绍了IIS请求里面的AppDomain之间的双向通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET应用程序,我们希望添加在一些请求来调用自定义脚本的能力。既然我们不相信这个剧本,我们正在产卵孩子的AppDomain这限制了我们的权限要求IIS里面,并同时装入了客户组件和脚本运行组件。客户脚本可以做的事情一样改变一个业务对象的描述或更改状态code到一个错误,如果满足一定的条件。因为变化是如此的不同,我不能在被从一个方法调用返回一个对象包括他们。在脚本运行时,我需要它来改变这种推出这个孩子的请求里面对象的值。

I'm working on an ASP.NET app, and we want to add the ability to call customer script during some of the requests. Since we don't trust this script, we are spawning a child AppDomain inside of our IIS request which has limited permissions, and loading both the customer assembly and a Script Runner assembly. The customer script can do things like change the description of a business object or change a status code to an error if certain criteria are met. Because the changes are so varied, I can't encompass them in a single object that gets returned from a method call. As the script is running, I need it to alter values in objects inside of the Request that launched this child.

帖子建议使用NetNamedPipeBinding,但我越来越担心,这是不恰当的code IIS,其中多个请求可以同时运行的内部运行。我会成立为每个IIS申请一个新的主机?建立了全过程的静态主机,然后使用端点以与正确的请求确保正确的孩子会谈?这是正确的技术?

This post recommends using NetNamedPipeBinding, but I'm growing concerned that it isn't appropriate for code running inside of IIS where multiple requests could be running concurrently. Would I set up a new host for each IIS request? Set up a static host for the entire process, and then use endpoints to make sure that the correct child talks with the correct request? Is this the right technology?

如果这不是正确的技术是什么?这<一个href=\"https://social.msdn.microsoft.com/Forums/vstudio/en-US/48119559-0a6c-4367-b34b-1e0826429a77/finding-the-default-appdomain-of-a-process-from-child-appdomain?forum=csharpgeneral\"相对=nofollow>帖子告诉您如何检索父的AppDomain的把手,但许多那些似乎使用mscoree.dll中,一般我是IM pression的COM和IIS没下T搭配非常好。 这是关于消灭您最初的AppDomain,并创建一个新的帖子会谈。也许这是没有必要在IIS中?

If this isn't the right technology, what is? This post tells you how to retrieve the handle of the parent AppDomain, but many of those seem to use mscoree.dll, and generally I was under the impression that COM and IIS didn't mix terribly well. This post talks about wiping out your initial AppDomain and creating a new one. Perhaps that's not necessary in IIS?

是否有一个孩子的AppDomain有效地执行产生它的Request对象的内部方法的一种方式,如果是的话是什么呢?

Is there a way for a child AppDomain to efficiently execute methods inside of the Request object that spawned it, and if so what is it?

推荐答案

我想我已经找到了一个简单的技术对一个孩子的的AppDomain ,以改变在父状态的的AppDomain 不使用WCF或MSCorEE.dll中。重读上面的第一篇文章,我看他确实提到它作为一种可能性,但拒绝它对于不是很清楚,我的理由。关键是要创建一个从派生的对象的 MarshalByRefObject的在父,获得的对象句柄即可,然后传递对象句柄孩子< STRONG>的AppDomain 。那么孩子可以解开手柄来获得一个代理,然后可以调用,这将改变在父对象的状态的方法。这里有一个code样品:<​​/ P>

I think I've found a simple technique for a child AppDomain to alter the state in the parent AppDomain without using WCF or mscoree.dll. Rereading the first post above, I see he does mention it as a possibility, but rejects it for reasons that are not clear to me. The trick is to create an object derived from MarshalByRefObject in the parent, obtain an ObjectHandle to it, and then pass that ObjectHandle to the child AppDomain. The child can then unwrap the handle to get a proxy, and can then call methods which will alter the state of the object in the parent. Here's a code sample:

class Program
{
    public class ParentFacade : MarshalByRefObject
    {
        public List<string> Collection { get; set; }
        public void AddElement(string el)
        {
            Collection.Add(el);
        }
    }

    public class ChildFacade : MarshalByRefObject
    {
        public void AlterParentState(ObjectHandle handle)
        {
            var parent = handle.Unwrap() as ParentFacade;
            parent.AddElement("Message from child");
        }
    }

    static void Main(string[] args)
    {
        var child = AppDomain.CreateDomain("child");
        var childFacadeType = typeof(ChildFacade);
        var childProxy = child.CreateInstanceAndUnwrap(childFacadeType.Assembly.FullName, childFacadeType.FullName) as ChildFacade;

        var parentColl = new List<string>();
        var facade = new ParentFacade();
        facade.Collection = parentColl;
        var facadeHandle = new ObjectHandle(facade);
        childProxy.AlterParentState(facadeHandle);

        foreach (var item in parentColl)
            Console.WriteLine(item);
    }
}

这code表明,在孩子的方法是能够改变在父对象的状态。这个特殊的例子并不十分有用的,但是这是非常强大的,当你建立一个沙箱中,并包括在您不信任的小孩议会.dll文件的。不可信的组件可调用的方法ChildFacade,从而改变在一个可控和安全的方式父的状态。

This code shows that a method in the child was able to alter the state of objects in the parent. This particular example isn't terribly useful, but this is very powerful when you are setting up a sandbox, and include .dll's in the child assembly that you don't trust. The untrusted assembly can call methods in the ChildFacade and thereby alter the state in the parent in a controlled and safe way.

这篇关于IIS请求里面的AppDomain之间的双向通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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