简体   繁体   English

C#GetRequestStream()Outlook插件“操作已超时”

[英]C# GetRequestStream() outlook add-in “The operation has timed out”

Good day. 美好的一天。

I really need help on this issue. 我真的需要这个问题的帮助。 I have tried every possible option here. 我在这里尝试了所有可能的选择。

I use a REST API in an Outlook add-in using C#. 我在使用C#的Outlook加载项中使用REST API。 The code links outlook items to CRM records, one way. 该代码以一种方式将Outlook项目链接到CRM记录。 The add-in works 100% fine but after a couple of calls outs i keep on getting the error "The operation has timed out". 加载项可以100%正常运行,但是在几次调用后,我继续收到错误消息“操作已超时”。

When I use the Google Chrome App "Advanced REST Client" I can post the same request 50 times after each other with no time out error. 当我使用Google Chrome应用“高级REST客户端”时,我可以相互发送50次相同的请求,而不会发生超时错误。

From within the add-in I use POST, GET and PATCH HttpWebRequest and I get the error for all of them. 从加载项中,我使用POST,GET和PATCH HttpWebRequest,并且收到所有错误。 The error happens at the code line System.IO.Stream os = req.GetRequestStream(); 该错误发生在代码行System.IO.Stream os = req.GetRequestStream();中。

Below is the method: 方法如下:

public static string HttpPatch(string URI, string Parameters) 
{

var req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URI);

    if (GlobalSettings.useproxy.Equals("true"))
    {
        req.Proxy = WebRequest.DefaultWebProxy;
        req.Credentials = new NetworkCredential(GlobalSettings.proxyusername,          GlobalSettings.proxypassword, GlobalSettings.proxydomain);
        req.Proxy.Credentials = new NetworkCredential(GlobalSettings.proxyusername,     GlobalSettings.proxypassword, GlobalSettings.proxydomain);
    }


req.Headers.Add("Authorization: OAuth " + GlobalSettings.token.access_token);
req.ContentType = "application/json";
req.Method = "PATCH";
byte[] data = System.Text.Encoding.Unicode.GetBytes(Parameters);
req.ContentLength = data.Length;
using (System.IO.Stream os = req.GetRequestStream())
{
    os.Write(data, 0, data.Length);
    os.Close();
}

WebResponse resp;

try
{
    resp = req.GetResponse();
}
catch (WebException ex)
{

    if (ex.Message.Contains("401"))
    {
    }
}
}

I suspect the problem is that you're not disposing of the WebResponse . 我怀疑问题在于您没有处置WebResponse That means the connection pool thinks that the connection is still in use, and will wait for the response to be disposed before reusing it for another request. 这意味着连接池认为该连接仍在使用中,并且将在将响应重新用于另一个请求之前等待响应被释放。 The connection is needed in order to get a request stream, and it won't become available unless the finalizer happens to kick in at a useful time, hence the timeout. 连接是获得请求流所必需的,除非终结器恰好在有用的时间启动,否则超时,否则连接将不可用。

Simply change your code using the response to use a using statement - or in your case, potentially something a little more complicated using a finally block as you're assigning it within a try block. 只需使用响应来更改代码,以使用using语句-或者在您的情况下,使用finally块可能会有些复杂,因为您要在try块中分配代码。 (We can't really see how you're using the response, which makes it hard to give sample code around that. But fundamentally, you need to dispose it.) (我们真的看不到您如何使用响应,这使得很难给出示例代码。但是从根本上讲,您需要处理它。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM