简体   繁体   English

简单的VB到C#和网络服务错误

[英]simple VB to C# and webservice error

I have a simple web service method which I'm trying to convert: 我正在尝试转换一个简单的Web服务方法:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
Public Function GetEmployees() As Object
    Dim _db As New DataClasses1DataContext()

    Return New With {.data = _db.Employees}
End Function

And this is my C# code: 这是我的C#代码:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public object GetEmployees()
{
    DBModelDataContext _db = new DBModelDataContext();
    return new { data = _db.Employees };
}

But when I run my service I get an error: 但是,当我运行我的服务时,我得到一个错误:

System.InvalidOperationException: Wystąpił błąd podczas generowania dokumentu XML. --->
System.InvalidOperationException: Aby serializacja XML była możliwa, dla typ&#243;w dziedziczących po elemencie IEnumerable należy zaimplementować metodę Add(System.Object) na wszystkich poziomach hierarchii dziedziczenia. Dla elementu System.Data.Linq.Table`1[[Scheduler.db.Employee, Scheduler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] metoda Add(System.Object) nie jest implementowana.
w System.Xml.Serialization.TypeScope.GetEnumeratorElementType(Type type, TypeFlags& flags)
w System.Xml.Serialization.TypeScope.ImportTypeDesc(Type type, MemberInfo memberInfo, Boolean directReference)
w System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
w System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
w System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_anyType(Object o)
w Microsoft.Xml.Serialization.GeneratedAssembly.ObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
w System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
--- Koniec śladu stosu wyjątków wewnętrznych ---
w System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
w System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
w System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
w System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
w System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
w System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
w System.Web.Services.Protocols.WebServiceHandler.Invoke()

I'm stuck for 4 hours now with this :/ 我被困了四个小时:/

I have a simple sql server database with 2 tables Employee and Plans. 我有一个简单的sql server数据库,其中有2个表Employee和Plans。

I need this kind of format to work with Ext Scheduler. 我需要这种格式才能使用Ext Scheduler。 VB code is from demo on Ext Scheduler webpage, I just want to convert it to C#. VB代码来自Ext Scheduler网页上的演示,我只想将其转换为C#。

EDIT: I'm using Visual Studio 2008 (.NET 3.5) and the demo in VB was created in 2010(.NET 4.0). 编辑:我使用的是Visual Studio 2008(.NET 3.5),而VB中的演示是在2010(.NET 4.0)中创建的。

尝试

return new { data = _db.Employees.ToList() };

The problem was result format. 问题是结果格式。 XML crashed, but JSON works. XML崩溃了,但是JSON有效。 I've needed that webservice to return JSON data, so problem solved (partially) 我需要该Web服务来返回JSON数据,因此(部分解决了)问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM