0

好的,我已经陷入这个错误很长一段时间了。这就是我正在做的事情。我创建了一个服务库并添加了我的服务实现逻辑。之后,我将它托管在 WCF 应用程序中。但我不断收到错误

服务器在处理请求时遇到错误

尝试浏览服务时。

这是我的代码:

[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;
    }
}

好的,当我尝试按照我指定的 uri 模板使用 URL 列出所有问题时:

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

我得到那个错误

注意:我只是在本地机器的 IIS 上测试这个服务。

4

0 回答 0