我有一个用c#编写的IIS托管的Web应用程序。在服务调用上,它会创建一个新的AppDomain并在加载后执行一个dll。当执行时间超过10分钟时,我收到以下的Remoting例外:
System.Runtime.Remoting.RemotingException:Object '/a3178091_1732_4272_b0bd_dae31e328450/lwntwiaw53jg1rd9gbvsigbe_25.rem' 已断开连接或在服务器上不存在。
服务器堆栈跟踪:
在 System.Runtime.Remoting.Channels.ChannelServices.CheckDisconnectedOrCreateWellKnownObject(即时聊天 MSG)
在 System.Runtime.Remoting.Channels.ChannelServices.SyncDispatchMessage(即时聊天 MSG)
在[0]处重新抛出异常:
在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天 reqMsg,IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&安培; msgData,Int32类型)
在System.IO.Stream.set_Position(Int64 value)
我创建应用域的方式如下
private AppDomain CreateDomainForTaskExecution(string baseDirectory)
{
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = baseDirectory;
domainSetup.PrivateBinPath = baseDirectory;
return AppDomain.CreateDomain("Domain for task execution", null, domainSetup);
}
然后我在服务调用中调用远程对象。
newDomain = CreateDomainForTaskExecution(domainBaseDir);
IExecutableTaskFactory taskLocator = (IExecutableTaskFactory)newDomain.CreateInstanceFromAndUnwrap(taskAssemblyFile.FullName, this.ExecutableTaskFactoryFullName);
IExecutableTask executableTask = taskLocator.Create();
IList<uint> ouputArguments = executableTask.OutputArgumentNames;
IDictionary<uint, Stream> taskOutput = new Dictionary<uint, Stream>();
foreach (uint argument in ouputArguments)
{
MarshalByRefObject stream = new MemoryStream();
taskOutput[argument] = stream as Stream;
ILease lease = (ILease)stream.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.Zero;
//lease.SponsorshipTimeout = TimeSpan.FromMinutes(30);
//lease.RenewOnCallTime = TimeSpan.FromMinutes(30);
}
lease.Register(this);
}
// Register as a sponsor.
foreach (Stream stream in inputArguments.Values)
{
ILease lease = (ILease)stream.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.Zero;
//lease.SponsorshipTimeout = TimeSpan.FromMinutes(30);
//lease.RenewOnCallTime = TimeSpan.FromMinutes(30);
}
lease.Register(this);
}
executableTask.Execute(inputArguments, taskOutput);
return taskOutput;
taskLocator是MarshalByRefObject并覆盖InitializeLifetimeService()函数,如下所示:
public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
if (lease.CurrentState == LeaseState.Initial)
{
lease.InitialLeaseTime = TimeSpan.Zero;
//lease.SponsorshipTimeout = TimeSpan.FromMinutes(30);
//lease.RenewOnCallTime = TimeSpan.FromMinutes(30);
}
return lease;
}
executableTask也是MarshalByRefObject,同样试图获得无限制的Lease,如上所述。
我还试图让我的服务对象成为一个ISponsor,并在Renew I do
中 public TimeSpan Renewal(ILease lease)
{
return TimeSpan.FromMinutes(5);
}
但永远不会召唤续约。我查了谷歌上的所有链接和堆栈溢出但找不到解决方案。可能出现什么问题?