Web services MVC4 Web服务错误
以下是错误: 无法序列化成员 BaicadicunnamChang.Areas.admin.Models.Role.Users类型的用户 System.Collections.Generic.ICollection`1[[baicadicunnamtchang.Areas.admin.Models.User, Baicadicunnamtang,版本=1.0.0.0,文化=中性, PublicKeyToken=null]],因为它是一个接口 错误发生在webservice.asmx文件中的函数getSong(int-id)处 下面是详细的代码: 角色类别:Web services MVC4 Web服务错误,web-services,asp.net-mvc-4,Web Services,Asp.net Mvc 4,以下是错误: 无法序列化成员 BaicadicunnamChang.Areas.admin.Models.Role.Users类型的用户 System.Collections.Generic.ICollection`1[[baicadicunnamtchang.Areas.admin.Models.User, Baicadicunnamtang,版本=1.0.0.0,文化=中性, PublicKeyToken=null]],因为它是一个接口 错误发生在webservice.asmx文件中的函数g
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.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; }
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用baicadicunnanthang.Areas.admin.Models.Category;
使用baicadicunnamtchang.Areas.admin.Models.Person;
使用黄芩提取物;
命名空间bacadicungnamthang.Areas.admin.Models.SongBase
{
公共类歌曲:SongBase
{
[关键]
public int SongID{get;set;}
公共虚拟ICollection编写器{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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用baicadicunnamtang.Areas.admin.Models.SongBase;
使用黄芩提取物;
白卡迪村南昌
{
///
///webservice的摘要描述
///
[WebService(命名空间=”http://baicadicungnamthang.net/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类webservice:System.Web.Services.webservice
{
私有BACADICUNNAMTShangContext db=新的BACADICUNNAMTShangContext();
[网络方法]
公共字符串HelloWorld()
{
返回“你好世界”;
}
[网络方法]
公共歌曲getSong(int id)
{
Song-Song=db.Songs.Find(id);
返回歌曲;
}
}
}
无法序列化接口。这个问题的答案会解释为什么。
另外请看。我知道原因是模型角色包含ICollection类型的用户,该用户是一个界面。但解决办法是什么?我无法更改模型成员的类型(ICollection)。
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;
}
}
}