0
votes

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. 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:

    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.

Now, I don't know if it has something to do with .Net Framework as I am using VS2010 with .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();
1
Can you please verify if the URL you are trying for httpwebRequest is working properly or not. If the URL is not accessible the HttlWebrequest.Create(url) will throw exception. - jadavparesh06
yes i did. it works fine.. By the way, the code does not even throw exception. - Udit Narayan
I would suggest you should use try catch blocks around your Execute() routine. - jadavparesh06
As I already said, it does not even throw an Exception. - Udit Narayan

1 Answers

0
votes

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.