Cannot access a disposed object issue

Topics: General, Troubleshooting, Writing modules
Sep 6, 2012 at 6:08 PM
Edited Sep 6, 2012 at 6:09 PM

I have created a Scheduled task and it works ok, however if I enable/disable modules etc it generates the following error,

Cannot access a disposed object.
Object name: 'TransactionScope'.

The code in my class is ;

 

using System;
using System.Linq;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Logging;
using Orchard.Tasks.Scheduling;

namespace TaskTest.Services
{

    public class MySyncService : IScheduledTaskHandler
    {
        private const string TaskType = "TestSync";
        private readonly IScheduledTaskManager _taskManager;
        public ILogger Logger { get; set; }
        private readonly IContentManager _contentManager;
        private readonly IOrchardServices _services;

        public MySyncService(IScheduledTaskManager taskManager, IContentManager contentManager, IOrchardServices services)
        {
            _taskManager = taskManager;
            _contentManager = contentManager;
            _services = services;

            Logger = NullLogger.Instance;
            try
            {
                DateTime first = DateTime.Now.AddMinutes(1);
                ScheduleTask(first);
            }
            catch (Exception ex)
            {
                this.Logger.Error(ex.Message);
            }

        }

        public void Process(ScheduledTaskContext context)
        {
            if (context.Task.TaskType == TaskType)
            {
                try
                {
                    // Do something

                }
                catch (Exception ex)
                {
                    this.Logger.Error(ex.Message);
                }
                finally
                {
                    DateTime nextTime = DateTime.Now.AddMinutes(2);
                    ScheduleTask(nextTime);
                }
            }
        }

        private void ScheduleTask(DateTime date)
        {
            if (date.ToUniversalTime() > DateTime.Now.ToUniversalTime())
            {
                var tasks = _taskManager.GetTasks(TaskType);
                if (tasks == null || tasks.Count() == 0)
                {

                    _taskManager.CreateTask(TaskType, date.ToUniversalTime(), null);

                }
            }
        }

    }
}

That's it - no other code.
Enabled the module, all seems ok, but say try and enable the Search module and bam!
Any ideas?
Coordinator
Sep 12, 2012 at 6:50 PM

A full stack trace would be helpful. Did you try to debug into it?

Sep 20, 2012 at 1:29 PM

Yes tried debugging into it but couldnt figure out why it was doing it.

How do i get a full stack trace - using VS 2010

 

Sep 20, 2012 at 1:32 PM

Is this what you wanted?

 	[External Code]	
>	Orchard.Framework.DLL!Orchard.Data.TransactionManager.System.IDisposable.Dispose() Line 43 + 0xd bytes	C#
 	[External Code]	
 	Orchard.Framework.DLL!Orchard.Environment.DefaultOrchardHost.DisposeShellContext() Line 204 + 0x16 bytes	C#
 	Orchard.Framework.DLL!Orchard.Environment.DefaultOrchardHost.MonitorExtensions.AnonymousMethod__6(Orchard.Caching.AcquireContext<string> ctx) Line 189 + 0x8 bytes	C#
 	Orchard.Framework.DLL!Orchard.Caching.Cache<string,string>.CreateEntry(string k, System.Func<Orchard.Caching.AcquireContext<string>,string> acquire) Line 57 + 0x14 bytes	C#
 	Orchard.Framework.DLL!Orchard.Caching.Cache<string,string>.UpdateEntry(Orchard.Caching.Cache<string,string>.CacheEntry currentEntry, string k, System.Func<Orchard.Caching.AcquireContext<string>,string> acquire) Line 33 + 0xde bytes	C#
 	Orchard.Framework.DLL!Orchard.Caching.Cache<string,string>.Get.AnonymousMethod__1(string k, Orchard.Caching.Cache<string,string>.CacheEntry currentEntry) Line 21 + 0x1d bytes	C#
 	[External Code]	
 	Orchard.Framework.DLL!Orchard.Caching.Cache<string,string>.Get(string key, System.Func<Orchard.Caching.AcquireContext<string>,string> acquire) Line 17 + 0x122 bytes	C#
 	Orchard.Framework.DLL!Orchard.Caching.DefaultCacheManager.Get<string,string>(string key, System.Func<Orchard.Caching.AcquireContext<string>,string> acquire) Line 33 + 0x69 bytes	C#
 	Orchard.Framework.DLL!Orchard.Environment.DefaultOrchardHost.MonitorExtensions() Line 185 + 0x4b bytes	C#
 	Orchard.Framework.DLL!Orchard.Environment.DefaultOrchardHost.BeginRequest() Line 213 + 0x8 bytes	C#
 	Orchard.Framework.DLL!Orchard.Environment.DefaultOrchardHost.Orchard.Environment.IOrchardHost.BeginRequest() Line 78 + 0xb bytes	C#
 	Orchard.Web.DLL!Orchard.Web.MvcApplication.HostBeginRequest(System.Web.HttpApplication application, Orchard.Environment.IOrchardHost host) Line 38 + 0x9 bytes	C#
 	Orchard.WarmupStarter.DLL!Orchard.WarmupStarter.Starter<Orchard.Environment.IOrchardHost>.OnBeginRequest(System.Web.HttpApplication application) Line 68 + 0x1d bytes	C#
 	Orchard.Web.DLL!Orchard.Web.MvcApplication.Application_BeginRequest() Line 29 + 0x10 bytes	C#
 	[External Code]	

 


Coordinator
Sep 21, 2012 at 8:56 PM

Yes, but I don't see anything in there that would explain the problem. Your code isn't even in that stack trace. Sorry, I have no idea what's causing that.

Editor
Apr 27, 2013 at 6:18 PM
I am seeing this issue as well. I added a scheduled task in one of my modules and now whenever I install or uninstall any module I get this error. Any resolution @rpter?