【问题标题】:WCF/REST Something is up with my POST?WCF/REST 我的 POST 有什么问题吗?
【发布时间】:2012-04-22 12:24:01
【问题描述】:

当我使用我的 Windows 窗体应用程序发布(第二次代码和平)时,当我通过 buttonclick 2 进入我的数据网格中获取学生集合时,没有任何显示我可以对成员进行硬编码并且获取没有问题,但我不能发布?当我为我的帖子单击 button1 时,我得到的回复在消息框中显示 OK?所以不太清楚我做了什么......

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "")]
    void AddStudent(Student student);

            XDocument xDoc = XDocument.Load(uri);
            var students = xDoc.Descendants("Student")
                .Select(n => new
                {
                    StudentNo = n.Element("StudentID").Value,
                    Firstname = n.Element("FirstName").Value,
                    Surname = n.Element("LastName").Value
                })
                .ToList();

            dataGridView1.DataSource = students;
        }

【问题讨论】:

  • 与命名StudentIDStudentNoFirstNameFirstnameLastNameSurname保持一致
  • 嘿,丹尼斯,这纯粹是为了向用户展示视觉效果
  • var students 是否已填充?如果是这样,请尝试使用BindingContext
  • 该方法不起作用仍然是 3 名学生的相同回报。我不确定它是不是我在我的操作合同中的附加学生在做它?
  • 在您类似的question 中,我看到您的xml 的根元素是ArrayOfStudent。为什么要手动形成 xml 而不是使用 xml 解析器? Student 类不用于序列化/反序列化有什么用?

标签: c# wcf web-services rest


【解决方案1】:

您是否在每次呼叫激活模式下运行?如果是这样,每个客户端请求都会获得一个新的专用服务实例,因此您的 List<> 每次都会被重新创建为空。

看到这个reference article。您必须在调用之间将您的列表保存在缓存或数据库中。

【讨论】:

  • 天哪!为了找到答案,我读了这么多,当然我知道这将是一行之类的事情:[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
【解决方案2】:

您的服务正在执行您在代码中编写的内容。 ListStudents 不断返回相同的学生列表,AddStudent 对列表不做任何事情。

【讨论】:

    【解决方案3】:

    您的 AddStudent 未添加到列表中

    public void AddStudent(Student student)
    {
        student.StudentID.ToString();
        student.FirstName.ToString();
        student.LastName.ToString();
    }
    

    GetStudentCollection 返回相同的硬编码值。

    static List<Student> students = new List<Student>();
    public void AddStudent(Student student)
    {
       students.Add(student);
    
    }
    
    public List<Student> GetStudentCollection()
    {
        return students;
    }
    

    【讨论】:

    • 仍然没有返回,也没有错误?顺便说一句,我最初有这个,但我也想要硬编码的成员(为了方便)
    • 那么可能还有其他问题。您的 Addstudent 可能无法正常工作。你能在那里放一个断点,看看是否被一个格式正确的学生调用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多