Created
September 22, 2014 15:22
-
-
Save pcibraro/1dadbf99eff02df7ed90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
using (Isolated<Work> isolated = new Isolated<Work>()) | |
{ | |
isolated.Value.CallEdge(); | |
} | |
} | |
} | |
public class Work : MarshalByRefObject | |
{ | |
public void CallEdge() | |
{ | |
var msg = "Hello world"; | |
var t = CallEdgeAsync(msg); | |
t.Wait(); | |
} | |
public async Task<IDictionary<string, object>> CallEdgeAsync(string msg) | |
{ | |
var load = Edge.Func(@" | |
return function (msg, callback) { | |
callback(null, { prop : 'foo' }); | |
} | |
"); | |
var rawresult = await load(msg); | |
return (IDictionary<string, object>)rawresult; | |
} | |
} | |
public sealed class Isolated<T> : IDisposable where T : MarshalByRefObject | |
{ | |
private AppDomain _domain; | |
private T _value; | |
public Isolated() | |
{ | |
_domain = AppDomain.CreateDomain("Isolated:" + Guid.NewGuid(), | |
null, AppDomain.CurrentDomain.SetupInformation); | |
Type type = typeof(T); | |
_value = (T)_domain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName); | |
} | |
public T Value | |
{ | |
get | |
{ | |
return _value; | |
} | |
} | |
public void Dispose() | |
{ | |
if (_domain != null) | |
{ | |
AppDomain.Unload(_domain); | |
_domain = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment