Click here to Skip to main content
14,639,661 members

How to upload data using windows services C#

abdul subhan mohammed asked:

Open original thread
Dear All,

I'm trying to create windows service to upload data from the client machine to the server using Ajax/web-server to SQL database.

Here, the window's services will create one log file.

The file on the client's machine is encrypted, so the windows services will first decrypt and then it will upload data onto the server. And there is one communication helper to upload data from the client's machine to the server.

The services are successfully getting the file and decrypting, but not uploading the data onto the server.

What I have tried:

public string UploadOfflineResult()
        {
            var pathSouce = Directory.GetParent(Directory.GetParent(Environment.SystemDirectory.ToString()).ToString()).FullName;
            var offlinePath = pathSouce + ConfigurationManager.AppSettings["resultFile"];
            WriteToFile(offlinePath);
            FileInfo _file = new FileInfo(offlinePath);
            WriteToFile(_file.Exists ? "Offline result file exist" : "Offline result file does not exist");


            if (_file.Exists && getPing())
            {
                WriteToFile("Working ON offline exam result file");
                OfflineExamResult offlineExamResult = null;
                try
                {
                    FileStream fs = new FileStream(@"" + offlinePath, FileMode.Open);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd();
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("CS: " + offlineExamResult.CandidateSessionID.ToString());
                        WriteToFile("Offline result file DECRYPTED");

                        //Not able to Upload data CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService(
                            JsonConvert.SerializeObject(offlineExamResult, Formatting.None, new JsonSerializerSettings
                            {
                                NullValueHandling = NullValueHandling.Ignore
                            }));

                        WriteToFile("UpdatedOfflineCandidateExamResult: CandidateSessionID=" + offlineExamResult.CandidateSessionID + " ");
                        return true.ToString();
                    }
                }
                catch (Exception ex)
                {
                    ex = ex.InnerException; //Avoid warning
                    return JsonConvert.SerializeObject(
                        new { success = false, message = "Invalid result file content" },
                        Formatting.None,
                        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
                }



            }
            return false.ToString();
        }


CommunicationHelper.cs
public static string UpdateCandidateOfflineExamResultUsingWindowService(string _offlineExamResult, string fullUrl)
       {
           WebClient webClient = new WebClient();
           webClient.Encoding = Encoding.UTF8;

           string postData = string.Format("offlineExamResult={0}", _offlineExamResult);
           string result = Task.Run(() => webClient.UploadStringTaskAsync(fullUrl, postData)).Result;

           return true.ToString();
       }


Can anyone please help me.


Thanks
Tags: C#5, Windows, service

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100