英文:
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+&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>
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+&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>
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
如果 bSetReminder
为 false
,则将 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,
...
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论