延迟声明StreamWriter变量

时间:2019-05-15 17:16:43

标签: c# streamwriter

在C#应用程序中,我有一个while循环,该循环从Redis消息队列中收集一个字符串,并将其发送到侦听服务器。 在每个存储单元,都使用HttpWebRequest Post方法打开连接,并使用StreamWriter变量发送数据。 问题是:发送两个字符串后,应用程序将冻结,而不会返回任何错误,它可能在一分钟内什么也不做,之后它又可以正常工作,并继续处理另外两个字符串,冻结等等。 调试显示了在声明StreamWriter变量期间发生的延迟。

这是代码的核心:

// configure Redis
var redis = new RedisClient("127.0.0.1");

while (true)
{
    // read from Redis queue
    string json = redis.BRPop(30, "sensors_data");

    //...
    //URL DECLARATION
    //...

    try
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentLength = json.Length;
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Proxy = null;

            SendDataAsync(json, url);
        }
    }

    static async Task SendDataAsync(string json, string url)
    {
        try
            {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentLength = json.Length;
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Proxy = null;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                try
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                    Console.Write("Data Sent");
                }
                catch (Exception e)
                {

                    Console.WriteLine(e.Message);
                }
            }

        }
        catch (Exception err)
        {
            Console.Write(err.Message);
        }
        Console.WriteLine();
    }

所以代码实际上可以正常工作,只是在声明StreamWriter时有一些奇怪的巨大延迟。有人有什么主意吗?我不知道该怎么处理。

编辑

        while (true)
        {
            i = 0;
            // read from Redis queue
            string json = redis.BRPop(30, "sensors_data");

            try
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                    httpWebRequest.ContentLength = json.Length;
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Method = "POST";
                    httpWebRequest.Proxy = null;
                using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        try
                        {
                            streamWriter.Write(json);
                            streamWriter.Flush();
                            streamWriter.Close();
                            var response = httpWebRequest.GetResponse();
                            StreamReader reader = new StreamReader(response.GetResponseStream());
                            string reply=reader.ReadToEnd();
                            Console.WriteLine(reply);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
                    }
            }
                catch (Exception err)
                {
                    Console.Write(err.Message);
                }

                Console.WriteLine();
        }

1 个答案:

答案 0 :(得分:0)

找到了解决方案:显然,在添加httpWebRequest.GetResponse()之后,必须从服务器上获取答案,接下来的两行我不再发现延迟。

建议:最好使用WebClient,从第一次尝试开始就不会给我带来任何问题。