2013-05-01 79 views
0

以下是錯誤:MVC4 web服務錯誤

Cannot serialize member baicadicungnamthang.Areas.admin.Models.Role.Users of type System.Collections.Generic.ICollection`1[[baicadicungnamthang.Areas.admin.Models.User, baicadicungnamthang, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] because it is an interface.

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

回答

0

你不能序列化的接口。回答this的問題會解釋你爲什麼。 也請看this

+0

我知道原因是模型角色包含ICollection類型的用戶是一個接口。但是解決方案如何?我無法更改模型的成員類型(ICollection)。 – 2013-05-02 17:49:07