如何从静态WCF服务返回对象列表 [英] How do I return a list of objects from a restful WCF service

查看:82
本文介绍了如何从静态WCF服务返回对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的静态WCF服务返回一个对象列表。我正在使用实体框架。问题是,当WCF服务运行时,如果我在上面放置了调试器,则该调试器将被访问两次,并且出现 404 服务器未找到错误。为什么会这样呢?

I am trying to return a list of objects from my restful WCF service. I am using Entity Framework. The problem is that when the WCF service runs and if I put a debugger on that then the debugger is visited twice and I get a 404 server not found error. Why is this happening.?

如果我返回一个单独的 class(db table),它与其他<$ c没有关系$ c> class(DB table)然后返回列表,但是如果我必须返回 class(DB table),则得到一个404错误。

If I return a return a single class(db table) which is not having relation with other class(DB table) then it is returning the list but if I have to return a class(DB table) then I get a 404 error.

接口:

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

实现:

public List<Course> GetDataString(string value)
{
    int v = Convert.ToInt32(value);
    TestEntities1 t = new TestEntities1();
    List<Course> u = t.Courses.ToList();
    return u;
}

可能是什么问题。?在这里,课程内衬到学生表

What can be the problem.? Here Courses is lined to student table.

当我编写用于获取相同数据的ADO.NET代码时,它就可以正常工作。

When i write an ADO.NET code for fetching the same data, it works just fine.

然后调试器执行两次后,出现服务器找不到错误页面,URL更改为

Then after the debugger has executed twice I get a server not found error page and the URL changes from

http://localhost:5127

http://www.localhost.com:5127/Service1.svc/GetDataString/1/Service1.svc/GetDataString/1

我也启用了跟踪,并且遇到了以下异常。我不知道这个例外是什么。

also I enabled the trace and I am getting the following exception. I dont have any idea as to what this exception is. Please help.

System.Runtime.Serialization.SerializationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

堆栈跟踪中的异常消息:

Message in stack trace for exception:

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


推荐答案

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

By default, you cannot transfer an object as its base type between a WCF server and client. The DataContractSerializer will throw an exception when it attempts to serialize the base class, You can find good article regarding your problem in here

这篇关于如何从静态WCF服务返回对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆