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();