我正在创建一个C#控制台应用程序,我必须在其中触发一个http url以在托管的Solr索引上启动一个进程。 我正在解雇的网址是
http://ServerName:8888/solr/people-dev/dataimport?command=full-import&clean=true
我正在使用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;
}
不,每当我尝试通过将上面的URL作为参数传递来调用此函数时,只要我的控件到达第一行,它就会尝试创建请求,等待1-2秒然后终止我正在运行的应用程序实例
现在,我不知道它是否与.Net Framework有关,因为我正在使用带有.Net 4.0的VS2010。寻求一些帮助..
添加问题,在父任务内创建的子任务内调用此函数。
父任务是否可能在子任务完全执行之前到期。如果是这样,如何确保它不会发生..
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();
答案 0 :(得分:0)
您的申请似乎没有任何问题。一切都完成后,控制台应用程序就会关闭。将Console.ReadKey()添加到应用程序的末尾,它将等到您按下按钮关闭。