简体   繁体   English

在控制台应用程序中创建HTTPWebRequest时出现Wierd C#错误

[英]Wierd C# Error while creating a HTTPWebRequest in a Console Application

I am creating a C# Console Application in which I have to fire a http url to start a process on a hosted Solr Index. 我正在创建一个C#控制台应用程序,在该应用程序中,我必须启动http URL才能在托管的Solr索引上启动进程。 The url I am firing is 我触发的网址是

http://ServerName:8888/solr/people-dev/dataimport?command=full-import&clean=true

I am using System.Net.WebRequest class for creating HTTPWebRequest like this: 我正在使用System.Net.WebRequest类来创建HTTPWebRequest,如下所示:

    public string Execute(string url)
    {                
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        request.Method = "GET";
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream strm = response.GetResponseStream();
        string res = String.Empty;
        using (StreamReader reader = new StreamReader(strm))
        {
            res = reader.ReadToEnd();
        }
        return res;                        
    }

No, whenever I try to invoke this function by passing above URL as a parameter, as soon as my control reaches the first line, it tries to create request, wait for 1-2 seconds and then terminate my running instance of application. 不,每当我尝试通过传递URL作为参数来调用此函数时,只要控件到达第一行,它就会尝试创建请求,等待1-2秒,然后终止我正在运行的应用程序实例。

Now, I don't know if it has something to do with .Net Framework as I am using VS2010 with .Net 4.0. 现在,我不知道它是否与.Net Framework有关,因为我正在将VS2010与.Net 4.0一起使用。 Looking for some help.. 寻求帮助。

Adding to the question, this function is called inside a child task created inside a parent task. 在问题之外,此函数在父任务内创建的子任务内调用。

Is that possible that parent task expire before child task execute completely. 父任务可能在子任务完全执行之前到期。 If so, how to make sure it does not happen.. 如果是这样,如何确保它不会发生。

Task parentImportTask = new Task(() => {

    TaskFactory tf = new TaskFactory(TaskCreationOptions.AttachedToParent,
        TaskContinuationOptions.ExecuteSynchronously);

    Task<bool> dataDumpTask = tf.StartNew<bool>(() =>
    {

        string sqlQuery = @"[dbo].[ImportKonnectPeopleDataForIndexing]";

        using (SqlConnection connection = DBConnection.getSqlConnection("KonnectDataDumpDB"))
        {
            int importStatus = -1;

            try
            {
                SqlCommand command = new SqlCommand(sqlQuery, connection);
                command.CommandType = System.Data.CommandType.StoredProcedure;
                command.CommandTimeout = 900;
                importStatus = command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Logger.log("Exception occured in data import task Solr/People::Index");
                throw e;
            }
            finally
            {  connection.Close(); }

            if (importStatus > 0)
            { return true; }
            return false;
        }
    }, TaskCreationOptions.AttachedToParent).ContinueWith((i) =>
    {
        if (!i.Result)
        { throw new Exception("Data Dump task not successfull Solr/People::Index"); }

        return solrConnector.Execute(url);

    }, TaskContinuationOptions.OnlyOnRanToCompletion);                       

}, TaskCreationOptions.LongRunning);

parentImportTask.RunSynchronously();
parentImportTask.Wait();

There seems to be nothing wrong with your application. 您的应用程序似乎没有错。 Console applications close as soon as everything is done. 一切完成后,控制台应用程序将关闭。 Add Console.ReadKey() to the end of you application and it will wait until you have pressed a button to close. 将Console.ReadKey()添加到应用程序的末尾,它将等待您按下按钮关闭。

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

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