title: Exe destroys itself
body: <p>I have made an console app (exe) with C#. Application is used to send some http requests to server. It sends json to call server's API to execute some work. I save cookies in environmental variable.</p>
  
  <p>We have noticed some weird behavior with this exe. With some sequence(we were not able to find out what sequence this is) of API calls ( api is called by command line arguments) the exe destroys itself. Its size becomes 0KB from starting 15MB. I have never seen that kind of behavior before. Do you have any idea what could it be?</p>
  
  <p>This is the main part of code that creates http request, and other uploads file...</p>
  
  <pre><code> static public JObject UploadFile(string fileName)
      {
  
  
          HttpWebRequest requestToServer = (HttpWebRequest)WebRequest.Create(serverDomain + directory + "/File");
  
  
          string boundaryString = "----SomeRandomText";
          requestToServer.AllowWriteStreamBuffering = false;
          requestToServer.Method = WebRequestMethods.Http.Post;
          requestToServer.ContentType = "multipart/form-data; boundary=" + boundaryString;
          requestToServer.KeepAlive = false;
          if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
          {
  
              CookieContainer cookies = new CookieContainer();
              cookies.Add(new Uri(serverDomain), new Cookie("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString()));
  
              requestToServer.CookieContainer = cookies;
          }
  
          ASCIIEncoding ascii = new ASCIIEncoding();
          string boundaryStringLine = "\r\n--" + boundaryString + "\r\n";
          byte[] boundaryStringLineBytes = ascii.GetBytes(boundaryStringLine);
  
          string lastBoundaryStringLine = "\r\n--" + boundaryString + "--\r\n";
          byte[] lastBoundaryStringLineBytes = ascii.GetBytes(lastBoundaryStringLine);
  
  
          string myFileDescriptionContentDisposition = String.Format(
              "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}",
              "myFileDescription",
              "A sample file description");
          byte[] myFileDescriptionContentDispositionBytes = ascii.GetBytes(myFileDescriptionContentDisposition);
  
          string fileUrl = fileName;
  
          string myFileContentDisposition = String.Format(
              "Content-Disposition: form-data;name=\"{0}\"; " + "filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n",
              "myFile", Path.GetFileName(fileUrl), Path.GetExtension(fileUrl));
          byte[] myFileContentDispositionBytes = ascii.GetBytes(myFileContentDisposition);
  
          FileInfo fileInfo = new FileInfo(fileUrl);
  
  
          long totalRequestBodySize = boundaryStringLineBytes.Length * 2 + lastBoundaryStringLineBytes.Length + myFileDescriptionContentDispositionBytes.Length + myFileContentDispositionBytes.Length + fileInfo.Length;
  
          requestToServer.ContentLength = totalRequestBodySize;
  
          using (Stream s = requestToServer.GetRequestStream())
          {
  
              s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
              s.Write(myFileDescriptionContentDispositionBytes, 0,
              myFileDescriptionContentDisposition.Length);
  
  
              s.Write(boundaryStringLineBytes, 0, boundaryStringLineBytes.Length);
              s.Write(myFileContentDispositionBytes, 0,
              myFileContentDispositionBytes.Length);
  
  
              FileStream fileStream = new FileStream(fileUrl, FileMode.Open,
              FileAccess.Read);
              byte[] buffer = new byte[1024];
              int bytesRead = 0;
              while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
              {
                  s.Write(buffer, 0, bytesRead);
              }
              fileStream.Close();
  
  
              s.Write(lastBoundaryStringLineBytes, 0, lastBoundaryStringLineBytes.Length);
          }
  
          WebResponse response = requestToServer.GetResponse();
          StreamReader responseReader = new StreamReader(response.GetResponseStream());
          string replyFromServer = responseReader.ReadToEnd();
          JObject jsonResult = JObject.Parse(replyFromServer);
          //CreateErrorLog(replyFromServer);
          CreateResponse(replyFromServer);
          return jsonResult;
      }
      static public JObject request(string json)
      {
  
  
  
          var httpWebRequest = (HttpWebRequest)WebRequest.Create(serverDomain + directory + "/json.rpc");
          httpWebRequest.ContentType = "text/json";
          httpWebRequest.Method = "POST";
  
          httpWebRequest.CookieContainer = new CookieContainer();
          if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
          {
  
              CookieContainer cookies = new CookieContainer();
              cookies.Add(new Uri(serverDomain), new Cookie("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString()));
  
              httpWebRequest.CookieContainer = cookies;
              httpWebRequest.Headers.Add("dopinus.e2.session", Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User).ToString());
          }
          using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
          {
  
              streamWriter.Write(json);
              streamWriter.Flush();
              streamWriter.Close();
  
              var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
              if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) == null)
              {
                  foreach (Cookie cook in httpResponse.Cookies)
                  {
                      Environment.SetEnvironmentVariable(cook.Name, cook.Value, EnvironmentVariableTarget.User);
                  }
              }
              else if (Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User) != null)
              {
                  foreach (Cookie cook in httpResponse.Cookies)
                  {
                      if (cook.Name == "dopinus.e2.session" &amp;&amp; cook.Value != Environment.GetEnvironmentVariable("dopinus.e2.session", EnvironmentVariableTarget.User))
                      {
                          Environment.SetEnvironmentVariable(cook.Name, cook.Value, EnvironmentVariableTarget.User);
  
                      }
                  }
  
              }
  
              using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
              {
                  var result = streamReader.ReadToEnd();
  
                  JObject jsonResult = JObject.Parse(result);
                  //CreateErrorLog(result);
                  CreateResponse(result);
                  return jsonResult;
              }
          }
      }
  </code></pre>