Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Updated 10-Jun-19 2:40am
v2
Comments
F-ES Sitecore 9-Jun-19 8:14am    
Where is the code that does the upload?
Dave Kreskowiak 9-Jun-19 10:56am    
You never showed the code for this call:
CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService

And you never explained what happens when this call is made or any error messages returned.

It's impossible for anyone to help you without that information.
abdul subhan mohammed 9-Jun-19 13:23pm    
To convert into the offlineResultInfo.cs
Dave Kreskowiak 9-Jun-19 13:40pm    
Your statement is meaningless. I have no idea what "offliceResultInfo.cs" is nor do I have any access to it because it's on your machine.
abdul subhan mohammed 9-Jun-19 13:25pm    
Got no errors and nothing happening. I checked the log file, its working till 'File Decrypted' but its not able to communicate the web server.

1 solution

You're not sending json formatted data to your webservice. You're sending "something={jsondata}". You don't do that. You send just the json formatted data, not the variable= part.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900