为什么在创建日历事件时不设置提醒(使用Microsoft Graph)会出现异常?

huangapple go评论78阅读模式
英文:

Why am I getting an exception when creating a calendar event with no reminder (using Microsoft Graph)?

问题

I have also asked this question on GitHub. This code raises an exception:

// Add the event
Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    Subject = oEvent.GetSubject(),
    Body = body,
    Start = startTime,
    End = endTime,
    IsAllDay = oEvent.IsAllDayEvent(),
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : (int?)null,
    Location = location,
    SingleValueExtendedProperties = extendedProperties,
    Sensitivity = oSettings.SetCalendarPrivate ? Sensitivity.Private : Sensitivity.Normal
});

I think the issue has to be with this line of code:

ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : (int?)null

Because, if bSetReminder is true, then I don't get an exception.

The exception happens when bSetReminder is false. The exception:

<LogEntry Date="2023-05-22 16:30:40" Severity="Exception" Source="OutlookCalIFConsole.Outlook+<AddEventToCalendar>d__18.MoveNext" ThreadId="6">
  <Exception Type="Microsoft.Graph.Models.ODataErrors.ODataError" Source="Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter+<ThrowIfFailedResponse>d__28.MoveNext">
    <Message>Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.</Message>
    <StackTrace>   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<ThrowIfFailedResponse>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.Me.Calendars.Item.Events.EventsRequestBuilder.<PostAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at OutlookCalIFConsole.Outlook.<AddEventToCalendar>d__18.MoveNext()</StackTrace>
  </Exception>
</LogEntry>

Please see linked question which has an answer. I am sure this used to work but in the recent Graph releases it now fails. Advice welcomed.

Update

I tried to obtain additional exception information but this provided no more info:

catch (Microsoft.Graph.Models.ODataErrors.ODataError ex)
{
    SimpleLog.Log(ex);
    SimpleLog.Log(ex.InnerException);
    return false;
}

As suggested in the answer, if I adjust the code:

Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    Subject = oEvent.GetSubject(),
    Body = body,
    Start = startTime,
    End = endTime,
    IsAllDay = oEvent.IsAllDayEvent(),
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : 0 /* (int?)null */ ,
    Location = location,
    SingleValueExtendedProperties = extendedProperties,
    Sensitivity = oSettings.SetCalendarPrivate ? Sensitivity.Private : Sensitivity.Normal
});

This will work with no exception. I agree that this is version 5 of the SDK, but when I lookup the definition of the property:

public int? ReminderMinutesBeforeStart {
    get { return BackingStore?.Get<int?>("reminderMinutesBeforeStart"); }
    set { BackingStore?.Set("reminderMinutesBeforeStart", value); }
}

It is still int? so I don't understand why (int?)null was no longer valid.

But, for now I will change it as the accepted answer. But, I would like more concrete explanation about why my code fails now in V5 of SDK.

英文:

I have also asked this question on GitHub. This code raises an exception:

// Add the event
Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    Subject = oEvent.GetSubject(),
    Body = body,
    Start = startTime,
    End = endTime,
    IsAllDay = oEvent.IsAllDayEvent(),
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : (int?)null,
    Location = location,
    SingleValueExtendedProperties = extendedProperties,
    Sensitivity = oSettings.SetCalendarPrivate ? Sensitivity.Private : Sensitivity.Normal
});

I think the issue has to be with this line of code:

ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : (int?)null

Because, if bSetReminder is true, then I don't get an exception.

The exception happens when bSetReminder is false. The exception:

<LogEntry Date="2023-05-22 16:30:40" Severity="Exception" Source="OutlookCalIFConsole.Outlook+<AddEventToCalendar>d__18.MoveNext" ThreadId="6">
  <Exception Type="Microsoft.Graph.Models.ODataErrors.ODataError" Source="Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter+<ThrowIfFailedResponse>d__28.MoveNext">
    <Message>Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.</Message>
    <StackTrace>   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<ThrowIfFailedResponse>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.<SendAsync>d__20`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.Me.Calendars.Item.Events.EventsRequestBuilder.<PostAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at OutlookCalIFConsole.Outlook.<AddEventToCalendar>d__18.MoveNext()</StackTrace>
  </Exception>
</LogEntry>

Please see linked question which has an answer. I am sure this used to work but in the recent Graph releases it now fails. Advice welcomed.


Update

I tried to obtain addition exception information but this provided no more info:

catch (Microsoft.Graph.Models.ODataErrors.ODataError ex)
{
    SimpleLog.Log(ex);
    SimpleLog.Log(ex.InnerException);
    return false;
}

As suggested in the answer, if I adjust the code:

Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    Subject = oEvent.GetSubject(),
    Body = body,
    Start = startTime,
    End = endTime,
    IsAllDay = oEvent.IsAllDayEvent(),
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : 0 /* (int?)null */ ,
    Location = location,
    SingleValueExtendedProperties = extendedProperties,
    Sensitivity = oSettings.SetCalendarPrivate ? Sensitivity.Private : Sensitivity.Normal
});

This will work with no exception. I agree that this is version 5 of the SDK, but when I lookup the definition of the property:

public int? ReminderMinutesBeforeStart {
    get { return BackingStore?.Get<int?>("reminderMinutesBeforeStart"); }
    set { BackingStore?.Set("reminderMinutesBeforeStart", value); }
}

It is still int? so I don't understand why (int?)null was no longer valid.

But, for now I will change it as the accepted answer. But, I would like more concrete explanation about why my code fails now in V5 of SDK.

答案1

得分: 2

如果 bSetReminderfalse,则将 ReminderMinutesBeforeStart 设置为 0 而不是 null

// 添加事件
Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    ...
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : 0,
    ...
});
英文:

If bSetReminder is false then set ReminderMinutesBeforeStart to 0 not to null.

// Add the event
Event createdEvent = await _graphClient.Me.Calendars[oSettings.CalendarID].Events.PostAsync(new Event
{
    ...
    IsReminderOn = bSetReminder,
    ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : 0,
    ...
});

huangapple
  • 本文由 发表于 2023年5月23日 00:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定