简体   繁体   English

Microsoft Project Online OData异步查询

[英]Microsoft Project Online OData asynchronous query

I am trying to use Microsoft Project OData by querying data in C#. 我正在尝试通过C#查询数据来使用Microsoft Project OData I am having performances issues with delays around 1s for each query. 我遇到性能问题,每个查询的延迟时间约为1秒。 I am trying to query 2 information at once using that method : 我试图使用该方法一次查询2条信息:

    public static async Task<string> ReadXml(string url)
    {
        var request = (HttpWebRequest)WebRequest.Create(url);

        request.Credentials = Credentials; // SharePointOnlineCredentials

        request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");

        using (var response = (HttpWebResponse)await request.GetResponseAsync())
        using (var stream = response.GetResponseStream())
        using (var reader = new System.IO.StreamReader(stream))
        {
            var xml = await reader.ReadToEndAsync();

            return xml;
        }
    }

It works fine if I call it and always wait for it to end before calling it again, but I never receive any response from the WebRequest if I call it multiple times at once : 如果我调用它并总是等待它结束再再次调用它,它会很好地工作,但是如果我一次调用它多次,我将永远不会收到来自WebRequest的任何响应:

// just an example. I usually put a condition to filter for the tasks of a single project
var query1 = ReadXml(@"https://something.sharepoint.com/sites/pwa/_api/ProjectData/Projects");
var query2 = ReadXml(@"https://something.sharepoint.com/sites/pwa/_api/ProjectData/Tasks");
Task.WaitAll(query1, query2);

If I "await" the first one and then do the second one it works fine, but not with the code above. 如果我“等待”第一个,然后再做第二个,则可以正常工作,但不能使用上面的代码。 And this is assuming there is < 300 tasks in the project, if more than that I have to query them in chunk of 300 leading to 4 or 5 seconds for the entire query since I can't to them all at once! 并假设项目中有<300个任务,如果超出这个数量,我必须以300个块的形式查询它们,导致整个查询花费4或5秒钟,因为我无法一次完成所有任务! Is there a way to send multiple request at the same time ? 有没有办法同时发送多个请求? I am able to do it by simply entering the url in multiple chrome tabs really fast / have faster responses. 我可以通过简单地在多个chrome标签中输入url来做到这一点,而且响应速度更快。 I don't understand why it doesn't work with my code! 我不明白为什么它不适用于我的代码!

Thanks, 谢谢,

According to the following post Webrequest.Create could be the problem, it uses an internally blocking method C# Thread UI is getting blocked | 根据下面的Webrequest.Create帖子,可能是问题所在,它使用了内部阻止方法C#Thread UI被阻止| Possible reason WebRequest.Create? WebRequest.Create的可能原因? .

The code below uses the newer HttpClient and shouldn't have this issue. 下面的代码使用较新的HttpClient,不应出现此问题。

    public static HttpClient _HttpClient { get; } = new HttpClient(new HttpClientHandler { Credentials=new NetworkCredential("","")});

    public static async Task<string> ReadXml(string url)
    {
        using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url))
        {
            requestMessage.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            using (var response = await _HttpClient.SendAsync(requestMessage))
            {
                return await response.Content.ReadAsStringAsync();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 IODataClientHandler 为 Microsoft.OData.Client 创建异步 SendingRequest2 事件处理程序 - Creating an asynchronous SendingRequest2 event handler for Microsoft.OData.Client with IODataClientHandler 从Microsoft Project Online中的custome字段获取Guid查找表 - Get Guid lookup table from custome Field in Microsoft Project Online Microsoft OData客户端 - Microsoft OData Client Microsoft CRM Online-比较日期和本地日期时的LINQ查询 - Microsoft CRM Online - LINQ query when comparing date to local date AspnetCore Odata 如何创建调用可等待查询的异步操作 - AspnetCore Odata How do create an asynchronous action calling an await-able query CRM 2015在线OData服务 - CRM 2015 Online OData Service 使用Microsoft Graph客户端sdk如何使用搜索Odata查询 - Using Microsoft Graph client sdk how would you use search Odata query Microsoft.AspNet.WebApi.OData与Microsoft.Data.OData和Microsoft.AspNet.OData有什么区别? - What is the difference between Microsoft.AspNet.WebApi.OData and Microsoft.Data.OData and Microsoft.AspNet.OData? 无法将Microsoft.Online.Administration.Automation.PSModule dll的引用添加到Visual Studio 2010中的项目中 - Unable to add the reference of the Microsoft.Online.Administration.Automation.PSModule dll to my project in Visual Studio 2010 swagger ui 中的 OData 查询 - OData query in swagger ui
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM