【问题标题】:msgraph: how to create a batch request for subscriptions using GraphServiceClient?msgraph:如何使用 GraphServiceClient 创建批量订阅请求?
【发布时间】:2021-03-25 20:47:46
【问题描述】:

我正在尝试创建一个批处理请求,该请求将在单个请求中创建多个图形通知订阅。

我一直在阅读这篇文章: https://docs.microsoft.com/en-us/graph/sdks/batch-requests?tabs=csharp

问题

我收到以下错误消息,但我不知道如何解决:

(local variable) Task<Subscription> userRequest
Argument 1: cannot convert from 'System.Threading.Tasks.Task<Microsoft.Graph.Subscription>' to 'Microsoft.Graph.BatchRequestStep'

代码

        var batchRequestContent = new BatchRequestContent();
        GraphServiceClient graphClient = await GenerateGraphAuthToken(this.amParms);

        foreach (string userId in this.UserstoSubscribe)
        {
                var subscription = new Subscription
                    {
                        ChangeType = "updated",
                        NotificationUrl= notificationURL,
                        Resource = $"users/{userId}/drive/root",
                        ExpirationDateTime = DateTimeOffset.Parse(subscriptionDate),
                        ClientState = "secretClientValue",
                        LatestSupportedTlsVersion = "v1_2"
                    };
                    var userRequest =  graphClient.Subscriptions
                        .Request()
                        .AddAsync(subscription);
                    var userRequestId = batchRequestContent.AddBatchRequestStep(userRequest);
        }
       var returnedResponse = await graphClient.Batch.Request().PostAsync(batchRequestContent);

任何提示将不胜感激。对不起。我只是 .NET 和 MS Graph 的新手。

【问题讨论】:

    标签: c# asp.net microsoft-graph-api


    【解决方案1】:

    AddBatchRequestStep 方法需要 BatchRequestStepIBaseRequestHttpRequestMessage 作为参数。 在您的情况下,您有一个POST 请求,因此您必须获取HttpRequestMessage 并转换为POST

    var subscription = new Subscription
    {
        ChangeType = "updated",
        NotificationUrl= notificationURL,
        Resource = $"users/{userId}/drive/root",
        ExpirationDateTime = DateTimeOffset.Parse(subscriptionDate),
        ClientState = "secretClientValue",
        LatestSupportedTlsVersion = "v1_2"
    };
    // create a json content from the subscription
    var jsonSubscription = graphClient.HttpProvider.Serializer.SerializeAsJsonContent(subscription);
    
    // create a HttpRequestMessage, specify the method and add the json content
    var userRequest = graphClient.Subscriptions.Request().GetHttpRequestMessage();
    userRequest.Method = HttpMethod.Post;
    userRequest.Content = jsonSubscription;
    
    // add userRequest to a batch request content
    var userRequestId = batchRequestContent.AddBatchRequestStep(userRequest);
    

    【讨论】:

    • 在这种情况下什么是“客户”?对不起。我超级绿。啊,也许是graphClient
    • @dot 是的,它是 graphClient。编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 2016-03-20
    • 2022-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2019-10-09
    相关资源
    最近更新 更多