我正在编写一个支持mod的游戏,为了安全起见,我将mods沙盒化为游戏引擎中的单独AppDomain(因此我可以将mods的功能与引擎分开限制)。但是,脚本域中的对象引擎引用了过早收集垃圾的引用,我得到了这样的异常:
对象 '/30b08873_4929_48a5_989c_e8e5cebc601f/lhejbssq8d8qsgvuulhbkqbo_615.rem' 已断开连接或在服务器上不存在。
在服务器端,我正在创建这样的对象(使用AppDomainToolkit:
// Take in a "script reference" which is basically just the name of a type in the scripting domain
public Reference<T> Dereference<T>(ScriptReference reference) where T : MarshalByRefObject
{
// Create a remote instance of this type
T instance = (T)RemoteFunc.Invoke<ScriptReference, object>(_pluginContext.Domain, reference, r =>
{
var t = Type.GetType(r.TypeName, true, false);
return Activator.CreateInstance(t);
});
//Return a reference which keeps this object alive until disposed
return new Reference<T>(instance, reference.TypeName, reference.Name);
}
Reference&lt; T&gt; 是该对象的赞助商,只要它保持活跃状态,远程域中的对象也将保持活跃状态。这就是问题所在,我通过他们的引用访问对象,但我仍然得到“在服务器上断开连接”的异常。这是我的参考&lt; T&gt; :
的实现public sealed class Reference<T>
: IDisposable
where T : MarshalByRefObject
{
private InnerSponsor _sponsor;
public T RemoteObject
{
get { return _sponsor.RemoteObject; }
}
public string TypeName { get; private set; }
public string Name { get; private set; }
public Reference(T obj, string typeName = null, string name = null)
{
TypeName = typeName;
Name = name;
_sponsor = new InnerSponsor(obj);
}
~Reference()
{
if (_sponsor != null)
_sponsor.Dispose();
_sponsor = null;
}
public void Dispose()
{
_sponsor.Dispose();
GC.SuppressFinalize(this);
}
/// <summary>
/// Inner sponsor is the actual sponsor (and is kept alive by the remoting system).
/// If all references to Reference are lost, it will dispose the sponsor in its destructor
/// </summary>
private sealed class InnerSponsor
: MarshalByRefObject, ISponsor, IDisposable
{
private readonly ILease _lease;
private bool _isDisposed;
public readonly T RemoteObject;
private bool _isUnregistered = false;
public InnerSponsor(T obj)
{
RemoteObject = obj;
_lease = (ILease)obj.GetLifetimeService();
_lease.Register(this, SponsorTime());
}
public TimeSpan Renewal(ILease lease)
{
if (lease == null)
throw new ArgumentNullException("lease");
if (_isDisposed)
{
if (!_isUnregistered)
{
_lease.Unregister(this);
_isUnregistered = true;
}
return TimeSpan.Zero;
}
return SponsorTime();
}
private static TimeSpan SponsorTime()
{
return TimeSpan.FromMilliseconds(/*Some value fetched from configuration*/);
}
public void Dispose()
{
_isDisposed = true;
}
}
}
即使有一个实时的参考&lt; T&gt; 给他们而导致我的物品死亡,我做错了什么?
答案 0 :(得分:2)
使用.NET内置的ClientSponsor类。它非常稳定和简单。
这是大多数人在AppDomains上工作时所转向的。您的实现看起来很好,但可能就像SponsorTime()
没有从配置中返回正确的值一样简单。
答案 1 :(得分:1)
我在this StackOverflow post找到了一些似乎更简单的解决方案。
它包括告诉对象有无限的生命时间,这样做:
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
return null;
}
此解决方案并非一致,但该帖子为我们带来了更多有价值的答案。