我正在创建一个 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();