2013-07-30 47 views
1

我在使用jQuery AJAX从“支持AJAX的WCF服务”中的WebGet函数检索数据时遇到了问题。该服务代码如下所示:wcf/ajax模糊错误“error”

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Activation; 
using System.ServiceModel.Web; 
using System.Text; 

namespace SPA 
{ 
    [ServiceContract(Namespace = "")] 
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
    public class db 
    { 
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json) 
    // To create an operation that returns XML, 
    //  add [WebGet(ResponseFormat=WebMessageFormat.Xml)], 
    //  and include the following line in the operation body: 
    //   WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"; 
    [WebGet] 
    [OperationContract] 
    public IEnumerable<Geofence> GetGeofences() 
    { 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/json"; 
     var dc = new AtomnetDataContext(); 
     return dc.Geofences; 
    } 

    // Add more operations here and mark them with [OperationContract] 
    } 
} 

这是试图调用它的代码:放置一个断点

$(function() { 
    $.get("db.svc/GetGeofences", alert); 
}); 

内的服务方法的代码表明,它确实调用。我证实,通过将dc.Geofences.ToArray()实现成一个变量(未在示例中显示)成功获取数据。 Geofence是Linq2sql生成的类型。

将调用转换为明确的ajax调用$.ajax({ ... });将错误对象返回给错误函数,但其​​中包含的消息仅仅表示“错误”,这不如说明性的。

使用相当于Firebug的IE10检查网络流量显示该呼叫是“(中止)”。此问题已将设置为服务配置,因为该调用尽可能返回值。

看起来有一个序列化异常,然后是一个通信异常,这可能是相应的。

A first chance exception of type 'System.Runtime.Serialization.SerializationException' 
occurred in System.Runtime.Serialization.dll A first chance exception of type 
'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.Web.dll 

回答

0

的所有问题的根源是,加入严重命名实体SiteData到模型中之后,我把它改名为地理围栏的事实。如果你这样做,你会得到我描述的问题。如果你不这样做,一切都会变得h d。

我仍然要面对的另一个问题是,当我包含其他与SiteData有关系并且设计人员识别并实现的实体时,这也会产生序列化错误。手动删除关系将其排除,但我有最强烈的感觉,你可能也可以通过配置设置来解决这个问题。任何人都知道吗?

哦,你不能通过alert作为成功函数,传递一个像这样的本地函数似乎discombobulate jquery。 O'reilly jQuery Pocket Reference的p70示例不起作用。无论如何,IE10都不是。

序列化错误是循环的结果。多一点挖掘产生此消息:

System.Runtime.Serialization.SerializationException occurred 
    HResult=-2146233076 
    Message=Object graph for type 'SPA.SiteGroup' contains cycles and cannot be 
    serialized if reference tracking is disabled. 
    Source=System.Runtime.Serialization 
    StackTrace: 
     at System.Runtime.Serialization.XmlObjectSerializerWriteContext.OnHandleReference(XmlWriterDelegator xmlWriter, Object obj, Boolean canContainCyclicReference) 
    InnerException: 

现在的问题是如何启用参考跟踪。

这应该是端点行为的属性。

<behaviors> 
    <endpointBehaviors> 
    <behavior name="SPA.dbAspNetAjaxBehavior"> 
     <enableWebScript /> 
     <dataContractSerializer 
     preserveObjectReferences="true" 
     ignoreExtensionDataObject="false" 
     maxItemsInObjectGraph="99" /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

不幸的是,你不能做任何事情,所以合理的,因为System.ServiceModel.Configuration.DataContractSerializerElement不公开为DataContractSerializer的的preserveObjectReferences构造函数的参数。

我的阴谋论者怀疑这不是偶然的。 Entity Framework团队不喜欢Linq2Sql,因为它通过执行英里国家/地区的EF来困扰他们。

我发现了另一种解决方法:如果您不想删除关联,则可以选择它们并将Child Property属性设置为False。这会禁止生成子对象集合,然后您没有循环引用。