MVC4 webservice错误

时间:2013-05-01 17:24:18

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

这是错误:

  

无法序列化成员   baicadicungnamthang.Areas.admin.Models.Role.Users类型   System.Collections.Generic.ICollection`1 [bai​​cadicungnamthang.Areas.admin.Models.User,   baicadicungnamthang,Version = 1.0.0.0,Culture = neutral,   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;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您无法序列化界面。回答this问题将解释原因。 也请看this