Esempio n. 1
0
        // POST api/values
        public void Post([FromBody] Object value)
        {
            //strings.Add(value);
            DataClass001 dataClass001      = System.Text.Json.JsonSerializer.Deserialize <DataClass001>(value.ToString());
            string       insertNewRowQuery = @"INSERT INTO MyTable (Key,Value) Values ('"
                                             + dataClass001.Key + "','" + dataClass001.Value + "')";

            using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=|DataDirectory|/databaseFile2.db3"))
            {
                using (System.Data.SQLite.SQLiteCommand com = new System.Data.SQLite.SQLiteCommand(conn))
                {
                    conn.Open();                            // Open the connection to the database

                    com.CommandText = insertNewRowQuery;    // Set CommandText to our query that will insert a row into the table
                    com.ExecuteNonQuery();                  // Execute the query

                    /*com.CommandText = "Select * FROM MyTable";      // Select all rows from our database table
                     *
                     * using (System.Data.SQLite.SQLiteDataReader reader = com.ExecuteReader())
                     * {
                     *  while (reader.Read())
                     *  {
                     *      Console.WriteLine(reader["Key"] + " : " + reader["Value"]);     // Display the value of the key and value column for every row
                     *  }
                     * }*/
                    conn.Close();        // Close the connection to the database
                }
            }
        }
        protected override void OnStart(string[] args)
        {
            //Create db
            WindowsServiceMentorshipTest.Database.InitializeDB.CreateDBTable();

            //Read from db
            string selectQuery = @"SELECT * FROM MyTable";

            using (System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection("data source=|DataDirectory|/databaseFile1.db3"))
            {
                using (System.Data.SQLite.SQLiteCommand com = new System.Data.SQLite.SQLiteCommand(conn))
                {
                    conn.Open();                       // Open the connection to the database

                    com.CommandText = selectQuery;     // Set CommandText to our query that will select all rows from the table
                    com.ExecuteNonQuery();             // Execute the query

                    using (System.Data.SQLite.SQLiteDataReader reader = com.ExecuteReader())
                    {
                        //string postJSON;
                        DataClass001 dataClass001 = new DataClass001();
                        while (reader.Read())
                        {
                            dataClass001.Key   = reader["Key"].ToString();
                            dataClass001.Value = reader["Value"].ToString();
                            //postJSON = JsonSerializer.Serialize(dataClass001);
                            System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result_Post_Keys.txt", dataClass001.Key);
                            System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result_Post_Values.txt", dataClass001.Value);


                            using (var client = new HttpClient())
                            {
                                client.BaseAddress = new Uri("http://webapi.local/api/");

                                //HTTP POST
                                //var postTask = client.PostAsJsonAsync<DataClass001>("values", dataClass001);
                                var postTask = client.PostAsJsonAsync <string>("values", @"{" + "\"key\":\"" + dataClass001.Key + "\"," + "\"value\":\"" + dataClass001.Value + "\"}");
                                postTask.Wait();

                                var result = postTask.Result;
                                if (result.IsSuccessStatusCode)
                                {
                                    //return RedirectToAction("Index");
                                    System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result_Post.txt", "Success" + result.StatusCode);
                                }
                                else
                                {
                                    System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result_Post.txt", "Fail" + result.StatusCode);
                                }
                            }
                        }
                        using (var client = new HttpClient())
                        {
                            client.BaseAddress = new Uri("http://webapi.local/api/");
                            //HTTP GET
                            var responseTask = client.GetAsync("values");
                            responseTask.Wait();

                            var result = responseTask.Result;
                            if (result.IsSuccessStatusCode)
                            {
                                System.IO.File.WriteAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result.txt", "Success");
                                var readTask = result.Content.ReadAsAsync <IList <string> >();
                                readTask.Wait();

                                IList <string> results = readTask.Result;

                                foreach (var s in results)
                                {
                                    //Console.WriteLine(d);
                                    System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\ResultBody.txt", s);
                                }
                                System.IO.File.AppendAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\ResultBody.txt", Environment.NewLine);
                            }
                            else
                            {
                                System.IO.File.WriteAllText(@"C:\Users\Isaiah\Documents\Mentorship Result Files\Result.txt", "FAIL");
                            }
                        }
                        conn.Close();        // Close the connection to the database
                    }
                }

                WriteToFile("Service is started at " + DateTime.Now);

                timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

                timer.Interval = 5000; //number in milisecinds

                timer.Enabled = true;
            }
        }