我有我想轉換一個簡單的Web服務方法:簡單的VB到C#和WebService錯誤
<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
這是我的C#代碼:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public object GetEmployees()
{
DBModelDataContext _db = new DBModelDataContext();
return new { data = _db.Employees };
}
但是當我運行我的服務我得到一個錯誤:
System.InvalidOperationException: Wystąpił błąd podczas generowania dokumentu XML. --->
System.InvalidOperationException: Aby serializacja XML była możliwa, dla typó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()
我現在很卡4小時這樣的:/
我有一個簡單的SQL服務器數據庫與2表員工和計劃。
我需要這種格式來使用Ext Scheduler。 VB代碼是來自Ext Scheduler網頁上的演示,我只是想將它轉換爲C#。我正在使用Visual Studio 2008(.NET 3.5),並在VB中演示在2010年(.NET 4.0)創建。
您最好返回一個員工或對象列表而不是對象。 – Doomsknight
這不是「只是轉換」。它正在切換回技術版本。我懷疑這個代碼足以解決你的問題。 –
這是一個選項,但Ext Scheduler使用這種格式。 – Misiu