.net 为什么我在创建没有提醒的日历事件(使用Microsoft Graph)时会出现异常?

z31licg0  于 2023-05-23  发布在  .NET
关注(0)|答案(1)|浏览(118)

我也在GitHub上问过这个问题。此代码引发异常:

// 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
});

我认为问题应该是这行代码:

ReminderMinutesBeforeStart = bSetReminder ? iReminderMinutes : (int?)null

因为,如果bSetRemindertrue,那么我不会得到异常。
bSetReminderfalse时发生异常。例外情况:

<LogEntry Date="2023-05-22 16:30:40" Severity="Exception" Source="OutlookCalIFConsole.Outlook+&lt;AddEventToCalendar&gt;d__18.MoveNext" ThreadId="6">
  <Exception Type="Microsoft.Graph.Models.ODataErrors.ODataError" Source="Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter+&lt;ThrowIfFailedResponse&gt;d__28.MoveNext">
    <Message>Exception of type 'Microsoft.Graph.Models.ODataErrors.ODataError' was thrown.</Message>
    <StackTrace>   at Microsoft.Kiota.Http.HttpClientLibrary.HttpClientRequestAdapter.&lt;ThrowIfFailedResponse&gt;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.&lt;SendAsync&gt;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.&lt;SendAsync&gt;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.&lt;PostAsync&gt;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.&lt;AddEventToCalendar&gt;d__18.MoveNext()</StackTrace>
  </Exception>
</LogEntry>

请看linked question,它有一个答案。我相信这曾经是有效的,但在最近的Graph版本中,它现在失败了。欢迎咨询。

jdg4fx2g

jdg4fx2g1#

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

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

相关问题