简体   繁体   中英

Can IoC and the Managed AddIn Framework (System.AddIn) work together with isolated AppDomains?

If I use Managed AddIn Framework (System.AddIn) and set it up to use separate AppDomains, can I use a centralized IoC container that is in the primary/default AppDomain? Can the IoC container resolve across the AppDomains?

I'm going to approach this answer by ignoring the MAF part of the equation, and concentrating on the AppDomain issue. An IoC container could theoretically do what you describe, assuming that the IoC entry point inherits from MarshalByRefObject or is wrapped by a class that in turn inherits from MarshalByRefObject. With a 29K+ rep score, I am sure you know this but:

1) Objects which inherit from MarshalByRefObject can be accessed across AppDomain boundaries via proxying (that is, all of the calls are marshalled across the appdomain boundary to the object).

2) Objects that are serializable can be passed across the AppDomain boundary via serialization, that is, you can get another copy of them in the other AppDomain.

For a number of reasons, you wouldn't want to serialize your whole IoC container and ship it across the AppDomain boundary. First off, the overhead of doing that would be enormous, and secondly there is likely a lot of plumbing behind an IoC container that isn't serializable. Therefore the only possible way for this to work is if:

1) The IoC container itself was MarshalByRef, or was wrapped by such

and

2) The objects that you were getting from the IoC container were all set up properly for cross-domain use (either serializable or MBR-inheriting).

If both of the above are true, then you could theoretically use the IoC container hosted in the main AppDomain from other AppDomains. You'd probably do this by defining a MAF host adapter that was specific to the IoC container's Resolve methods (or whatever the equivalent is in the IoC tool you use).

Keep in mind that a LOT of IoC functionality (especially AOP) is implemented using the same proxy APIs that cross-appdomain communication also uses. I could definitely see this complicating things if you attempt to use the IoC container for anything more than basic serializable structures and MBR-inheriting services.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM