HttpWebRequest runs slowly first time within SQLCLR

前端 未结 6 739
一生所求
一生所求 2021-02-06 14:47

When making an HttpWebRequest within a CLR stored procedure (as per the code below), the first invocation after the Sql Server is (re-)started or after a given (but indeterminat

6条回答
  •  我在风中等你
    2021-02-06 14:53

    I have tested and my first cold run (after SQL service restart) was in 3 seconds (not 30 as yours), all others are in 0 sec.

    The code sample I've used to build a DLL:

    using System;
    using System.Data;
    using System.Net;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.SqlTypes;
    using Microsoft.SqlServer.Server;
    
    namespace MySQLCLR
    {
        public static class WebRequests
        {
            public static void MakeWebRequest(string address, string parameters, int connectTO)
            {
                string returnData;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Concat(address.ToString(), "?", parameters.ToString()));
                request.Timeout = (int)connectTO;
                request.Method = "GET";
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(responseStream))
                        {
                            returnData = reader.ReadToEnd();
                            reader.Close();
                        }
                        responseStream.Close();
                    }
                    response.Close();
                }
                SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] { new SqlMetaData("response", SqlDbType.NVarChar, 10000000) });
                rec.SetValue(0, returnData);
                SqlContext.Pipe.Send(rec);
            }
        }
    }
    

提交回复
热议问题