C# 在单独的应用程序域中运行dll时,IIS承载的Web应用程序中出现RemotingException

C# 在单独的应用程序域中运行dll时,IIS承载的Web应用程序中出现RemotingException,c#,iis,remoting,C#,Iis,Remoting,我有一个IIS托管的web应用程序,是用c编写的。在服务调用中,它创建一个新的AppDomain,并在加载后执行dll。当执行时间超过10分钟时,我收到以下远程处理异常: System.Runtime.Remoting.RemotingException:对象 “/a3178091_1732_4272_b0bd_DAE33128450/lwntwiaw53jg1rd9gbvsigbe_25.rem” 已断开连接或服务器上不存在 服务器堆栈跟踪: 在 System.Runtime.Remoting

我有一个IIS托管的web应用程序,是用c编写的。在服务调用中,它创建一个新的AppDomain,并在加载后执行dll。当执行时间超过10分钟时,我收到以下远程处理异常:

System.Runtime.Remoting.RemotingException:对象 “/a3178091_1732_4272_b0bd_DAE33128450/lwntwiaw53jg1rd9gbvsigbe_25.rem” 已断开连接或服务器上不存在

服务器堆栈跟踪:

在 System.Runtime.Remoting.Channels.ChannelServices.CheckDisconnectedorCreateWellKnownObjectMessage 味精

在 System.Runtime.Remoting.Channels.ChannelServices.SyncDispatchMessageIMessage 味精

在[0]处重试异常:

在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessageIMessage reqMsg,IMessage retMsg

在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvokeMessageData& msgData,Int32类型

在System.IO.Stream.set_位置Int64值

我创建应用程序域的方式如下所示

    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;
executableTask也是一个MarshallByRefObject,类似地,它试图获得如上所述的无限制租约

我还试图使我的服务对象成为ISponsor,在续费时我做到了

    public TimeSpan Renewal(ILease lease)
    {
        return TimeSpan.FromMinutes(5);
    }

但是,永远不需要更新。我在Google和stack overflow上查找了所有链接,但找不到解决方案。可能的问题是什么?

我还使用ITrackingHandler接口发现MemoryStream对象正在断开连接。我使用了以下代码//当被跟踪对象断开连接时调用。[System.Security.Permissions.SecurityPermissionAttribute System.Security.Permissions.SecurityAction.LinkDemand,Flags=System.Security.Permissions.SecurityPermissionFlag.Infrastructure]公共void DisconnectedObjectObject对象{Console.WriteLineTracking:一个{0}实例已断开连接,。obj.ToString;}终于找到了解决方案,inputargs和taskoutput中的流已正确租用,但它们所属的字典未被租用。因此,创建了一个封装字典的类型,该类型具有IDictionary接口,并且是具有infine生存期的MarshallByRefObject。这修复了远程处理层中断开连接的对象错误。
    public TimeSpan Renewal(ILease lease)
    {
        return TimeSpan.FromMinutes(5);
    }