Two-way communication between AppDomains inside of IIS Requests(IIS 请求内的 AppDomain 之间的双向通信)
问题描述
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.
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?
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?
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?
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);
}
}
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 之间的双向通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:IIS 请求内的 AppDomain 之间的双向通信
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01