Search code examples
c#asp.netwcfweb-servicesinternal-server-error

Web Service Response returns a 500 Internal Server Error


I have written my own web service which works fine stand alone. I'm calling this web service from another page, and at that time it returns a 500 Internal Server error. I'm going through this process for the 1st time and do not know what this means or why this happens. A preliminary search on Google shows a wide range of answers, although I couldnt find anything specific.

Heres my calling code -

List<string> inputList = new List<string>();
inputList.Add("F");
inputList.Add("B");    
List<string> resultList = new List<string>();
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/Helpers/MyService.asmx");
httpWebRequest.Headers.Add("SOAPAction", "\"http://tempuri.org/GetOutput\"");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
JavaScriptSerializer serializer = new JavaScriptSerializer();

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = serializer.Serialize(inputList);
    streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); *****ERROR HERE

string responseText = String.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    responseText = streamReader.ReadToEnd();
}
resultList = serializer.Deserialize<List<string>>(responseText);

Heres my Web Service Code -

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class MyService: System.Web.Services.WebService
{        
    public string GetOutput(string jsonStr)
    {
        try
        {
            /*code to format jsonStr and send it along to another web service*/

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            string responseText = String.Empty;
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                responseText = streamReader.ReadToEnd();
            }

            //this obj has a head & body
            ResponseObj response_obj = new ResponseObj();                
            response_obj = serializer.Deserialize<ResponseObj>(responseText);                
            List<string> Values = new List<string>();
            if (response_obj.head.result)
                return serializer.Serialize(response_obj.body);

            return "";                  
        }
        catch (Exception ex)
        {
               throw new Exception(ex.Message);
        }
    }

Solution

  • I read that you can see details of the error at localhost/statistics/log, which showed me that there was an issue with my Web.config file. I commented the mentioned lines and the error went away making way for the next!