无法使用Graph API更新现有的日历事件

问题描述 投票:0回答:1

我正在尝试为现有事件更新以下字段,但未按预期更新。

我们要更新的字段:

  1. 添加和删除附件
  2. 更新主题和正文内容。
  3. 添加和/或删除强制/可选参与者。

使用以下代码删除附件:

await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Attachments[attachmentId]
    .Request()
    .DeleteAsync()
    .ConfigureAwait(false);

使用以下代码添加附件:

var fileAttachment = new Microsoft.Graph.FileAttachment
{
    ODataType = attachment.odataType,
    Name = attachment.name,
    ContentBytes = attachment.contentBytes,
    ContentType = attachment.contentType
};

var response = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Attachments
    .Request()
    .AddAsync(fileAttachment);

使用以下代码更新与会者:

var updateEvent = new Microsoft.Graph.Event
{
    Attendees = attendees
};

var resultUpdate = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Request()
    .UpdateAsync(updateEvent);

使用以下代码更新主题和正文内容:

var updateEvent = new Microsoft.Graph.Event
{
    HasAttachments = true,
    ResponseRequested = false,
    Subject = subject,
    Body = body
};

var resultUpdate = await graphClient
    .Users[organizer]
    .Events[organizerEventId]
    .Request()
    .UpdateAsync(updateEvent);

我正在按顺序执行上述代码,但是当我调试代码时,我发现它仅执行第一个逻辑以删除附件,并且调用而没有执行下面用相同方法编写的其余代码逻辑。

c# microsoft-graph microsoft-graph-sdks
1个回答
0
投票

我找到了此问题的根本原因并予以解决。实际上,问题在于异步函数调用。我试图从如下所示的非异步函数中调用异步函数:

public void CreateEvent(type parameter1, type parameter2)
{    
     objMyServiceClass.CreateEvent(parameter1, parameter2);
}

我将上面的代码更改为下面的代码,因为MyServiceClass中的CreateEvent是一个异步函数。现在它开始正常工作。

public void CreateEvent(type parameter1, type parameter2)
{    
     Task.Run(async () => await objMyServiceClass.CreateEvent(parameter1, parameter2));
}

我知道,这是一个非常愚蠢的错误,但有时我们的开发人员很难找到这样一个愚蠢的错误,并且最终陷入了噩梦。我希望它可以帮助某人。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.