我在WebService中创建了一个WebService ..并创建了Get方法..
我的代码是
public class WebService:System.Web.Services.WebService {
DataClassesDataContext db = new DataClassesDataContext(); public WebService () { //Console.log("Enter"); //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)] public object Get() { return db.Countries.ToList(); } }
但是当我在测试网络服务时,我得到了以下错误..
System.InvalidOperationException:生成错误时出错 XML文档。 ---> System.InvalidOperationException:类型 System.Collections.Generic.List`1 [[Country,App_Code.hlnie2jd, Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null]]可能不是 在这种情况下使用。在 System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(字符串 name,String ns,Object o,Boolean xsiType)at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(字符串 n,String ns,Object o,Boolean isNullable,Boolean needType)at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_anyType(对象 o)at Microsoft.Xml.Serialization.GeneratedAssembly.ObjectSerializer.Serialize(对象 objectToSerialize,XmlSerializationWriter writer)at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o,XmlSerializerNamespaces名称空间,String encodingStyle, String id)---内部异常堆栈跟踪结束--- at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o,XmlSerializerNamespaces名称空间,String encodingStyle, 字符串id)at System.Xml.Serialization.XmlSerializer.Serialize(的TextWriter textWriter,Object o,XmlSerializerNamespaces namespaces)at System.Xml.Serialization.XmlSerializer.Serialize(的TextWriter textWriter,Object o)at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse对象 响应,Stream outputStream,Object returnValue)at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(对象[] returnValues,Stream outputStream)at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(对象[] returnValues)at System.Web.Services.Protocols.WebServiceHandler.Invoke()
任何有解决方案的人请帮帮我
答案 0 :(得分:1)
这可能是因为您的方法签名将对象作为返回类型。
看看这个问题: ...may not be used in this context...while serialization
答案 1 :(得分:0)
您既不能返回列表,也不能返回服务中的对象类型。
但是你可以返回数组:
[WebMethod]
public Country[] Get()
{
// get you data
return data.ToArray();
}