Cannot serialize parameter of type 'System.Linq.Enumerable… ' when using WCF, LINQ, JSON

醉酒当歌 提交于 2019-11-29 07:00:44

No, one must return a concrete class from a web service. Make the return type List and be done with it.

You have to use the ServiceKnownTypes attribute.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Test.Service
{
    [ServiceContract(Name = "Service", Namespace = "")]
    public interface IService
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        [ServiceKnownType(typeof(List<EventData>))]
        IEnumerable<EventData> Method(Guid userId);
    }
}

Basically you need to know the concrete type of what you're returning. Pretty simple.

You need to pass interoperable collections to and from your WCF methods.

WCF plays best with simple types and arrays.

Pass in an array from your client and then turn it into an IEnumerable in the service.

Stuff like IEnumerable is not interoperable, which is what WCF is trying to be.

There may be a way to get around it with known types, but I always strive to make my components as flexible as possible.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!