【问题标题】:MVC4 webservice errorMVC4 网络服务错误
【发布时间】:2013-05-01 17:24:18
【问题描述】:

这是错误:

无法序列化成员 baicadicungnamthang.Areas.admin.Models.Role.Users 类型 System.Collections.Generic.ICollection`1[[baicadicungnamthang.Areas.admin.Models.User, baicadicungnamthang,版本=1.0.0.0,文化=中性, PublicKeyToken=null]] 因为它是一个接口。

webservice.asmx文件中getSong(int id)函数出错

这里是详细代码:

角色类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace baicadicungnamthang.Areas.admin.Models
{
    public class Role
    {
        [Key]
        public int RoleID { get; set; }
        public string Title { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }
}

用户类别:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using baicadicungnamthang.Areas.admin.Models.AlbumBase;
using baicadicungnamthang.Areas.admin.Models.Social;
using baicadicungnamthang.DAL;
using ICB;

namespace baicadicungnamthang.Areas.admin.Models
{
    public class User : Generic
    {
        [Key]
        public int UserID { get; set; }
        public virtual Role Role { get; set; }
    }
}

歌曲类:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using baicadicungnamthang.Areas.admin.Models.Category;
using baicadicungnamthang.Areas.admin.Models.Person;
using baicadicungnamthang.DAL;

namespace baicadicungnamthang.Areas.admin.Models.SongBase
{
    public class Song : SongBase
    {
        [Key]
        public int SongID { get; set; }
        public virtual ICollection<Composer> Composers { get; set; }
    }
}

webservice.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using baicadicungnamthang.Areas.admin.Models.SongBase;
using baicadicungnamthang.DAL;

namespace baicadicungnamthang
{
    /// <summary>
    /// Summary description for webservice
    /// </summary>
    [WebService(Namespace = "http://baicadicungnamthang.net/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class webservice : System.Web.Services.WebService
    {
        private baicadicungnamthangContext db = new baicadicungnamthangContext();

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public Song getSong(int id)
        {
            Song song = db.Songs.Find(id);
            return song;
        }
    }
}

【问题讨论】:

    标签: web-services asp.net-mvc-4


    【解决方案1】:

    您不能序列化接口。回答this 问题将解释您的原因。 看看this

    【讨论】:

    • 我知道原因是模型 Role 包含 ICollection 类型的 User 是一个接口。但解决方案如何?我无法更改模型成员的类型 (ICollection)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多