我有一些f#代码(下面)带有一个unit-> obj并在它自己的appdomain中对它进行评估。我的动机是我需要释放在评估期间加载的程序集。代码似乎工作正常,至少在我尝试过的琐碎的例子上。但是,当从NUnit测试调用时,它会失败并出现异常。例外和代码如下。我会非常感谢任何帮助。
Sandbox.trivial_test: System.Runtime.Serialization.SerializationException:Type不是 已解决会员'Sandbox + myDelegate @ 28,sandboxtest, Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null'。
module Sandbox
open System
type IsolationRunner (f:unit->obj) =
inherit System.MarshalByRefObject ()
let mutable storedResult : obj option = None
member x.callback () : unit =
let res = f ()
storedResult <- Some res
member x.result : obj =
match storedResult with
None -> failwith "No result yet"
| Some x -> x
let run_code_in_own_appdomain (f: unit -> obj) : obj =
let appDomain = AppDomain.CreateDomain "MyTempDomain"
try
let ir = IsolationRunner f
let myDelegate = new CrossAppDomainDelegate (fun () -> ir.callback ())
appDomain.DoCallBack myDelegate
ir.result
finally
AppDomain.Unload appDomain
open NUnit.Framework
[<Test>]
let trivial_test () =
let actual = run_code_in_own_appdomain (fun x -> 123 |> box) |> unbox
printf "result is %O\n" actual
Assert.AreEqual (123, actual)
答案 0 :(得分:1)
NUnit-Discuss的网友提出了一个解决方案。简而言之,只需让path成为可执行文件的位置(即目录),然后将另一个域创建为:
AppDomain.CreateDomain(“MyTempDomain”,null,path,“”,false)