如何从Web服务</dynamic>返回列表<dynamic>

时间:2014-08-20 14:50:06

标签: c# .net web-services serialization asmx

我有一个返回动态列表的函数,我想将其返回。这可能吗?

我的功能是

   public List<dynamic> GiveMeProjects(int id)
        {
            Entities.VSTMEntities vstm = new Entities.VSTMEntities();
            var currentUserProject = (from users in vstm.Users
                                      from project in users.Projects
                                      where users.UserID == id
                                      select new
                                      {
                                          Id = project.ProjectID,
                                          Name = project.ProjectName
                                      }).ToList<dynamic>();

            return currentUserProject;
        }

并将其称为

  [WebMethod()]
  public List<dynamic> GiveMeProjects(string id)
  {
     int pId = Convert.ToInt32(id);
     return projectItems.GiveMeProjects(pId);
   }

当我测试服务时,此函数正在生成异常

There was an error generating the XML document. ---> System.InvalidOperationException: f__AnonymousType102[System.Int32,System.String] cannot be serialized because it does not have a parameterless constructor. at System.Xml.Serialization.TypeDesc.CheckSupported() at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type) at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_ArrayOfAnyType(Object o) at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces) at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue) at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) at System.Web.Services.Protocols.WebServiceHandler.Invoke()

如何解决此问题

1 个答案:

答案 0 :(得分:1)

C#动态关键字不应该像这样使用。既然你现在将返回什么,正确的方法是创建一个类或结构并返回它。

public class ProjectOverview
{
   public int Id { get; set; }
   public int Name { get; set; }
}
public List<ProjectOverview> GiveMeProjects(int id)
        {
            Entities.VSTMEntities vstm = new Entities.VSTMEntities();
            var currentUserProject = (from users in vstm.Users
                                      from project in users.Projects
                                      where users.UserID == id
                                      select new ProjectOverview()
                                      {
                                          Id = project.ProjectID,
                                          Name = project.ProjectName
                                      }).ToList();

            return currentUserProject;
        }

动态关键字只应在特殊场合使用。您可以在officals MSDN article中找到详细信息。如果您不想这样做,可以检查现有的类,如KeyValuePair或Tuple。不建议这样做。服务需要一个清晰的界面,以便客户可以轻松访问它。最好通过将自定义类作为返回值公开(同时在某一点扩展您的接口)。