0

Ok, I've been stuck in this error for quite some time now. Here is what I'm doing. I created a service library and added my service implementation logic. After that, I'm hosting it in an WCF application. But I keep on getting an error

The server encountered an error processing the request

while trying to browse the service.

Here is my code:

[DataContract(Namespace = "http://Collect-Info.com/Questions")]
public class Question
{
    [DataMember]
    public int ID;
    [DataMember]
    public string question;
    [DataMember]
    public int CategoryID;
    [DataMember]
    public int TypeID;
}

[DataContract(Namespace = "http://Collect-Info.com/Answers")]
public class Answer
{
    [DataMember]
    public int ID;
    [DataMember]
    public int PID;
    [DataMember]
    public int QuestionID;
    [DataMember]
    public string QAnswer;
    [DataMember]
    public int CategoryID;
}

[ServiceContract()]
public interface IInfoCollectService
{
    [OperationContract()]
    [WebGet(UriTemplate = "Questions", ResponseFormat = WebMessageFormat.Json)]
    List<Question> GetAllQuestionList();

    [OperationContract()]
    [WebGet(UriTemplate = "Questions/{ID}", ResponseFormat = WebMessageFormat.Json)]
    List<Question> GetQuestionListByCategory(string id);

    [OperationContract()]
    [WebGet(UriTemplate = "Answers", ResponseFormat = WebMessageFormat.Json)]
    List<Answer> GetAllAnswerList();

    [OperationContract()]
    [WebGet(UriTemplate = "Answers/{ID}", ResponseFormat = WebMessageFormat.Json)]
    List<Answer> GetAnswerListByCategory(string id);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class InfoCollectService : IInfoCollectService
{
    public List<Question> GetAllQuestionList()
    {
        var dt = (new DBHandler()).GetResult("Select * From Question");
        var qQuestion = from t in dt.AsEnumerable()
                        select new Question()
                        {
                            ID = Int32.Parse(t["ID"].ToString()),
                            question = t["Question"].ToString(),
                            CategoryID = Int32.Parse(t["Category"].ToString()),
                            TypeID = Int32.Parse(t["Type"].ToString())
                        };

        return qQuestion.ToList<Question>();
    }

    public List<Question> GetQuestionListByCategory(string id)
    {
        var dt = (new DBHandler()).GetResult("Select * From Question where Category = " + id);
        if (dt.Rows.Count > 0)
        {
            var qQuestion = from t in dt.AsEnumerable()
                            select new Question()
                            {
                                ID = Int32.Parse(t["ID"].ToString()),
                                question = t["Question"].ToString(),
                                CategoryID = Int32.Parse(t["Category"].ToString()),
                                TypeID = Int32.Parse(t["Type"].ToString())
                            };

            return qQuestion.ToList<Question>();
        }
        return null;
    }

    public List<Answer> GetAllAnswerList()
    {
        var dt = (new DBHandler()).GetResult("Select * From Answer");
        var qAnswer = from t in dt.AsEnumerable()
                      select new Answer()
                      {
                          ID = Int32.Parse(t["ID"].ToString()),
                          PID = Int32.Parse(t["PID"].ToString()),
                          QuestionID = Int32.Parse(t["QuestionID"].ToString()),
                          QAnswer = t["QAnswer"].ToString(),
                          CategoryID = Int32.Parse(t["CategoryID"].ToString())
                      };

        return qAnswer.ToList<Answer>();
    }

    public List<Answer> GetAnswerListByCategory(string id)
    {
        var dt = (new DBHandler()).GetResult("Select * From Answer where QuestionID = " + id);
        if (dt.Rows.Count > 0)
        {
            var qQuestion = from t in dt.AsEnumerable()
                            select new Answer()
                            {
                                ID = Int32.Parse(t["ID"].ToString()),
                                PID = Int32.Parse(t["PID"].ToString()),
                                QuestionID = Int32.Parse(t["QuestionID"].ToString()),
                                QAnswer = t["QAnswer"].ToString(),
                                CategoryID = Int32.Parse(t["CategoryID"].ToString())
                            };

            return qQuestion.ToList<Answer>();
        }
        return null;
    }
}

Ok, when I just try to list all the questions using the URL as per my uri template specified :

http://www.domain.com/Service1.svc/Questions

I get that error

Note: I'm just testing this service on my local machine's IIS.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ahmii321
  • 41
  • 1
  • 10
  • 1
    Have you **debugged** through your `GetAllQuestionList` method? Where exactly does this error occur? Also: why are you doing this so complicated - getting a `DataTable` first, and then converting it to a list of `Question` objects?? Seems overly complicated and uses twice the memory needed - why not just use an ORM like Linq-to-SQL or Entity Framework in the first place and save yourself that extra step and extra memory of a intermediate `DataTable` :... – marc_s Aug 03 '13 at 09:41
  • 1
    In addition to debugging your service, you can also turn on trace logging: http://inaspiralarray.blogspot.com/2013/04/finding-root-cause-of-wcf-exceptions.html – Bob Horn Aug 03 '13 at 12:53
  • Why are you converting to int and then to string? That's seems to be an unnecessary step. Also, `Int32.Parse` with throw an error if the parse fails, and since you don't appear to have any error handling in your service that will bring down the service. – Tim Aug 03 '13 at 18:38
  • as @Tim mentioned, the proper way for using Service implementation is to handle service exceptions, please read: http://stackoverflow.com/questions/1536087/what-is-the-best-approach-to-handle-exceptions-in-wcf-service this is one of the most important things to keep a healthy state – ilansch Aug 03 '13 at 21:05
  • Hi, Thank you all for the suggestions. Yes, I've added few exceptional Handling now, Also added the LINQ to SQL for Data Accesses. About the other things of converting Data Table to List is going to be evicted out. However, My problem About the Exception is still not solved. The Service Runs fine when I run on the VS Development Server. It gives this Error When I try to Host it on the IIS ? Any suggestions ? DB issue ? any other issue ? – ahmii321 Aug 03 '13 at 22:30

0 Answers0