如何返回list从网络服务
本文关键字:dynamic 网络服务 list 何返回 返回 | 更新日期: 2023-09-27 18:19:00
我有一个函数返回动态列表,我想返回它。这可能吗?
我的功能是
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()
如何解决这个问题
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;
}
动态关键字只能在特殊场合使用。您可以在官方MSDN文章中找到详细信息。如果您不想这样做,您可以检查现有的类,如KeyValuePair或Tuple。但不建议这样做。服务需要一个清晰的接口,以便客户端可以轻松地访问它。最好的方法是将自定义类作为返回值公开(也可以在某一点上扩展接口)。
这不再是真的。可以有多态对象或常规对象,JSON序列化器将对它们进行序列化。我指的是。net6.0。此外,真正的流媒体已经在。net 6.0中启用,所以这将额外提供这个功能。
[HttpGet("execute")]
public async IAsyncEnumerable<dynamic> Get()
{
var extractor = new Extractor();
// different types of objects being returned in extract
await foreach (var extract in extractor)
{
yield return extract;
}
}