1

i'm trying to fetch all appointments from a Calendar. But after 15 minutes the fetching stops and im getting the following error message: Thread was being aborted.

here's my code:

  public List<CalendarObjectClass> GetCalendarObjects(string manualRoomList, out string message,
        out List<ModifiedObjectClass> modifiedCalObjListe, string selectedExchangeVersion, string[] roomList)
    {
        var calObjListe = new List<CalendarObjectClass>();

        message = "";

        _es = GetExchangeServiceBinding(selectedExchangeVersion);


        var view = new ItemView(1000);

        var extendedpropSet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Organizer,
            AppointmentSchema.RequiredAttendees, AppointmentSchema.Recurrence,
            AppointmentSchema.DeletedOccurrences, AppointmentSchema.ModifiedOccurrences,
            AppointmentSchema.LastOccurrence);

        // Add a search filter that searches on the body or subject.
        var searchFilterCollection = new List<SearchFilter>
        {
            new SearchFilter.IsGreaterThan(AppointmentSchema.Start, DateTime.Now.AddYears(-1))
        };
        // Create the search filter.
        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or,
            searchFilterCollection.ToArray());

        try
        {
            foreach (var room in roomList)
            {
                var appointments = _es.FindItems(new FolderId(WellKnownFolderName.Calendar, new Mailbox(room)),
                    searchFilter, view);

                foreach (var item in appointments)
                {
                    var appointment = (Appointment) item;
                    var extendedItem = Appointment.Bind(_es, appointment.Id, extendedpropSet);

                    byte[] value;
                    if (extendedItem.Recurrence != null)
                    {
                        if (extendedItem.LastOccurrence.Start < DateTime.Now)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (appointment.Start < DateTime.Now)
                        {
                            continue;
                        }

                    }
                    var calobj = new CalendarObjectClass();
                    calobj.vondatum = appointment.Start;
                    calobj.bisdatum = appointment.End;
                    calobj.smtp = room;
                    if (OhneVeranstalter())
                    {
                        calobj.veranstalteremail = Funktionsaccount.Email;
                        calobj.veranstaltername = Funktionsaccount.BenutzerName;
                    }
                    else
                    {
                        calobj.veranstalteremail = extendedItem.Organizer.Address;
                        calobj.veranstaltername = extendedItem.Organizer.Name;
                    }

                    calobj.isrecurring = appointment.IsRecurring;
                    calobj.attendeecollection = extendedItem.RequiredAttendees;
                    calobj.personenanzahl = extendedItem.RequiredAttendees.Count;
                    calobj.recurrence = extendedItem.Recurrence;
                    calobj.deletedoccurrences = extendedItem.DeletedOccurrences;
                    calobj.modifiedoccurrences = extendedItem.ModifiedOccurrences;
                    calobj.itemid = appointment.Id;

                    SetExtendedRecurrenceInformation(extendedItem.Recurrence, ref calobj);

                    appointment.Load(propset);


                    if (appointment.TryGetProperty(pidLidCleanGlobalObjectId, out value))
                    {
                        calobj.correlationid = Convert.ToBase64String(value);
                    }

                    if (calobj.recurrence.NumberOfOccurrences == null && calobj.isrecurring)
                    {
                        calobj.recurrence.NumberOfOccurrences = GetOccurences(_es, calobj);
                    }

                    if (OhneVeranstalter())
                    {
                        calobj.thema = appointment.Subject;
                    }
                    else
                    {
                        calobj.thema = GetSubjectFromUser(calobj);
                    }

                    if (calobj.thema != "SubjectError" &&
                        (calobj.recurrence.NumberOfOccurrences > 0 && calobj.isrecurring || calobj.isrecurring == false))
                    {
                        calObjListe.Add(calobj);
                    }
                }
                Log.Debug("Raum: " + room + " wurde ausgelesen");
                message += "Raum: " + room + " wurde ausgelesen" + "<br>";
            }
        }
        catch (Exception ex)
        {
            Log.Debug("Fehler: Beim Auslesen der Kalenderobjekte ist ein Fehler aufgetreten: " + ex.Message);
            message += "Beim Auslesen der Kalenderobjekte ist ein Fehler aufgetreten: " + ex.Message + "<br>";
        }
        modifiedCalObjListe = modObjListe;
        return calObjListe;
    }

Could you explain me what the problem is?

Is there a better solution to fetch the data from EWS.

Thanks in Advance...

Eray Geveci
  • 1,099
  • 4
  • 17
  • 39

1 Answers1

2

By default, the appdomain will shut down after a period of inactivity. When this happens, a ThreadAbortException will be thrown for any active threads. There are techniques to leave it always running or adjust the timeout. You will need to take advantage of one of those options to fix it.

By the way, it's not a good idea to have a single HTTP request take 15 minutes to process. You should handle this in an asynchronous manner. An initial request should start the long running job, then later you can get the job results. Scott Hanselman has a great blog post about background tasks in ASP.NET. I suggest you pick one of those approaches and base your method around it.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121