C# 如何从restful WCF服务返回对象列表

C# 如何从restful WCF服务返回对象列表,c#,entity-framework,wcf,wcf-rest,C#,Entity Framework,Wcf,Wcf Rest,我正在尝试从restful WCF服务返回对象列表。我使用的是实体框架。问题是,当WCF服务运行时,如果我在其上放置了一个调试器,那么调试器将被访问两次,并且我会得到一个404server not found错误。为什么会这样 如果我返回一个与其他类(db table)没有关系的类(db table),那么它将返回列表,但是如果我必须返回一个类(db table),那么我将得到一个404错误 接口: [OperationContract] [WebInvoke(Method = "GET", U

我正在尝试从restful WCF服务返回对象列表。我使用的是实体框架。问题是,当WCF服务运行时,如果我在其上放置了一个调试器,那么调试器将被访问两次,并且我会得到一个
404
server not found错误。为什么会这样

如果我返回一个与其他
类(db table)
没有关系的
类(db table)
,那么它将返回列表,但是如果我必须返回一个
类(db table)
,那么我将得到一个404错误

接口:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetDataString/{value}",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Course> GetDataString(string value);

此外,我还启用了跟踪,并得到以下异常。我不知道这个例外是什么。请帮忙

System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
堆栈跟踪中异常的消息:

Type 'System.Data.Entity.DynamicProxies.Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11' with data contract name 'Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
以下是我的课程:

namespace JSONWebService
{
    [DataContract]
    public partial class course
    {
        public Course()
        {
            this.Students = new HashSet<Student>();
        }

        [DataMember]
        public int C_Id { get; set; }

        [DataMember]
        public string C_Name { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }
}
名称空间JSONWebService
{
[数据合同]
公共部分课程
{
公共课程()
{
this.Students=newhashset();
}
[数据成员]
公共int C_Id{get;set;}
[数据成员]
公共字符串C_Name{get;set;}
公共虚拟ICollection学生{get;set;}
}
}

您的服务不知道如何序列化
课程
课程。您是否向其添加了
[DataContract]
[DataMember]
属性?您可以发布它的外观吗?

默认情况下,您不能在WCF服务器和客户端之间传输对象作为其基本类型。DataContractSerializer在尝试序列化基类时将引发异常,您可以在

中找到关于您的问题的好文章。您的web浏览器似乎找不到侦听localhost:5127的web服务器。确保它正在运行。(在VS.中按F5或Ctrl+F5)@abatishchev:不,当我编写用于获取相同数据的ADo.net代码时,它工作正常。获取数据的方式与web服务器的可用性无关。仔细检查它是否在运行。例如,放置一个断点并确保它被命中。此外,您可能会得到一个异常,导致web服务器进程停止,这是很有可能的。无论如何,请调试您的应用程序。查找导致异常的行(如果有)。我已将其添加到课程类中。我需要将其添加到课程类或学生类吗?@user2998990您需要将
[DataContact]
[DataMember]
也添加到学生类,或者需要添加
[KnownType(typeof(student)]
attribute@CoderofCode:[知识类型(类型)(学生)]在学生班或课程上。?@CoderofCode,tchrikch:我还没有编写这些类。这些类是在我添加实体数据模型(edmx)时自动生成的。请帮助。没有任何功能。Ado.net没有问题,因为它没有与类交互。Rite..?因为使用Ado.net可以获得正确的结果。WCF使用序列化将数据从服务器传输到客户端。但Ado.net没有。WCF和Ado.net是完全不同的概念。Ado.net用于从数据库获取数据,WCF用于传输数据从一端到另一端的数据。我知道..我要说的是..在WCF服务本身中,我没有使用EF和LINQ,而是使用ado.net代码(存储过程)来获取数据,并将SP返回的数据存储在数据集中,然后将其转换为列表,然后返回。在这种情况下,一切正常
System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Type 'System.Data.Entity.DynamicProxies.Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11' with data contract name 'Course_4B21E9E950AEFDC2233FE771C1BFE0ABF63D591A6487C93CBD145965FB96EA11:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
namespace JSONWebService
{
    [DataContract]
    public partial class course
    {
        public Course()
        {
            this.Students = new HashSet<Student>();
        }

        [DataMember]
        public int C_Id { get; set; }

        [DataMember]
        public string C_Name { get; set; }

        public virtual ICollection<Student> Students { get; set; }
    }
}