简体   繁体   English

WCF REST 服务抛出无法序列化类型错误的参数

[英]WCF REST Service throwing Cannot serialize parameter of type error

I know that something similar to this has been answered before but its not exactly what im getting.我知道之前已经回答过类似的问题,但这并不是我得到的。

I have a LINQ to Entity model consisting of a number of dynamically generated classes such as Shows, Users etc我有一个 LINQ to Entity 模型,其中包含许多动态生成的类,例如 Shows、Users 等

I have a WCF REST service which returns various objects.我有一个 WCF REST 服务,它返回各种对象。 For instance, i have the following method:例如,我有以下方法:

[WebGet(UriTemplate = "GetShow/{showid}")]
    public Ent.Shows GetShow(string showid)
    {
        using (var context = new Ent.choobEntities())
        {
            int showID = Convert.ToInt32(showid);
            List<Shows> shows = (from Shows in context.Shows
                          where Shows.Id == showID
                          select Shows).ToList();
            if (shows.Count > 0)
                return (Ent.Shows)shows.First();
            else
                return new Ent.Shows();
        }
    }

In the case above where a particular show doesnt get returned i create a new empty one and when testing with my REST client i get:在上面没有返回特定节目的情况下,我创建了一个新的空节目,当我使用 REST 客户端进行测试时,我得到:

    <Shows xmlns="http://schemas.datacontract.org/2004/07/Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Id>0</Id>
<ShowDescription i:nil="true"/>
<ShowImage i:nil="true"/>
<ShowTitle i:nil="true"/>
</Shows>

Thats perfect.那很完美。 However when i get a match i get the most irritating error, the然而,当我得到一场比赛时,我得到了最令人恼火的错误,

Cannot serialize parameter of type 'System.Data.Entity.DynamicProxies.Shows because it is not the exact type 'Entities.Shows' in the method signature and is not in the known types collection.无法序列化“System.Data.Entity.DynamicProxies.Shows”类型的参数,因为它不是方法签名中的确切类型“Entities.Shows”并且不在已知类型集合中。 In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute为了序列化参数,使用 ServiceKnownTypeAttribute 将类型添加到操作的已知类型集合中

Ive found suggestions to put [ServiceKnownType(typeof(<Shows>))] everywhere, above [ServiceContract] and KnownType etc in the class itself.我找到了将[ServiceKnownType(typeof(<Shows>))]放在任何地方的建议,在类本身的[ServiceContract]KnownType等之上。 NOTHING works.没有任何效果。 Surely there is a way without having to recreate a new instance of Shows everytime and copy the data over from the returned Entity into a new instance?当然有一种方法无需每次都重新创建 Shows 的新实例并将数据从返回的实体复制到新实例中? i don't believe that is the only way.我不相信这是唯一的方法。

Have you tried disabling dynamic proxies with:您是否尝试过禁用动态代理:

context.ContextOptions.ProxyCreationEnabled = false;

This is discussed under the "Serializing POCO Proxies" section here: http://msdn.microsoft.com/en-us/library/dd456853.aspx这在“序列化 POCO 代理”部分下讨论: http : //msdn.microsoft.com/en-us/library/dd456853.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM