c# - Execute function in external assembly loaded in different appdomain -
task
- load and/or unload external assembly @ runtime.
- execute code in external assembly, passing variables external code , expecting return value.
problem
there no way unload individual assembly without unloading of application domains contain it. use unload method
appdomain
unload application domains.
solution
create new appdomain
, load external assembly new appdomain
, execute whatever code need execute in external assembly , (optional) destroy new appdomain
afterwards.
problem 2
unfortunately process not trivial , requires use intermediary proxy object can invoke method in remote appdomain
without referencing object in local application domain in way (which again lock assembly local appdomain
).
solution 2
creating assembly proxy (or wrapper), derived marshalbyrefobject
, clr
can marshal reference across appdomain
boundaries.
i found this piece of code doing need do, but...
therefor have come stackoverflow ask can't seem figure out wrong, why , how fix it.
for trust me on this, loading of external assembly in new appdomain working fine, invocation of code in external assembly not.
here code have invoking external assembly
public class soapwebserviceinvoker { private readonly guid _assemblyid; private readonly iassemblymanager _assemblymanager; private readonly foo _foo; public soapwebserviceinvoker(iassemblymanager assemblymanager, guid assemblyid, string url) { _assemblymanager = assemblymanager; _assemblyid = assemblyid; _foo = new foo { url = url }; } public object invoke(string classname, string methodname, dictionary<string, object> methodparameters) { return _assemblymanager.reflect(_assemblyid, assembly => _foo.invoke(assembly, classname, methodname, methodparameters)); } }
previously code in foo
located in soapwebserviceinvoker
class, got exception saying: type 'soapwebserviceinvoker' in assembly '...' not marked serializable
, moved separate class see if fixed problem (because foo
should serializable
).
[serializable] public class foo { public string url { get; set; } public object invoke(assembly assembly, string classname, string methodname, dictionary<string, object> methodparameters) { var type = gettypeinassembly(assembly, url, classname); var method = getmethodoftype(type, url, classname, methodname); var parameters = method.getparameters(); if (method.declaringtype == null) throw new exception("decleringtype null methodinfo."); var instance = activator.createinstance(method.declaringtype); return method.invoke(instance, methodparameters.select(x => { var parametertype = parameters.single(y => y.name.equals(x.key)).parametertype; if (parametertype.isenum) { return enum.parse(parametertype, x.value.tostring()); } return convert.changetype(x.value, parametertype); }).toarray()); } private type gettypeinassembly(assembly assembly, string url, string classname) {...} private methodinfo getmethodoftype(type type, string url, string classname, string methodname) {...} }
but still getting serialization exception (but foo
): type 'foo' in assembly '...' not marked serializable
.
it may stupid mistake have overlooked, cant life of me find out , why won't work... :s
Comments
Post a Comment