recently, my production environment encountered a strange behavior which I cant understand why or what the reason behind it. I have the very same code implemented in my local environment, and I can debug it normally without any problem. it just seldom occurs in my production environment.
After further debugging my production environment, I found that the problem lies in the string format
.
So im building a string with the value is taken from the web.config
var HereIsTheProblem = string.Format("client_id={0}&username={1}&password={2}",
TakenFromConfig("abc"),
TakenFromConfig("bcd"),
TakenFromConfig("cde"));
when I try to print this HereIsTheProblem
variable, it wont even print anything, and its like every time the code encountered this variable, it would skip it and do nothing.
to check that the problem is not caused by the config, I take the value from the config, and put it into the string.Format
right away
var HereIsTheProblem = string.Format("client_id={0}&username={1}&password={2}",
"abcderfg-1231kasd-1231sad-znasda",
"username",
"password");
and it is still encountering the same behavior. After further debugging, I notice that the problem lies within the first value which has dashes. as long as it has 1 dash or more, it will encounter the said problem, but when I remove the dash, this problem will not happen.
to further explain how weird this is, is that this kind of problem suddenly occur in some time (without us deploy or update our code), and without us fixing it, it would recover itself after some time
Is there someone know what happen behind this code?
EDIT : this code is used in ASP.NET and put within IIS
EDIT2 : this is how I printed my variable into a log file. and this is how I printed it.
FileStream fs = File.Open(strFileName, FileMode.Append, FileAccess.Write,
FileShare.ReadWrite);
StreamWriter myWriter = new StreamWriter(fs);
myWriter.WriteLine(WriteAnyLogHere);
this variable also used as a request body to be sent through httpWebRequest
. and the HttpWebResponse
just come back with no object
EDIT 3 :
This is the actual code how I use my HereIsTheProblem
variable. logging is only for debugging it
string URL = "URL";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Method = "POST";
httpWebRequest.Timeout = 300000;
httpWebRequest.Accept = "text/html";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string HereIsTheProblem = "From Above Description";
streamWriter.Write(HereIsTheProblem);
streamWriter.Flush();
}
try
{
using (var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
}
catch (WebException ex)
{
using (var httpResponse = (HttpWebResponse)ex.Response)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
}