wcf / ajax模糊错误“错误”

时间:2013-07-30 00:48:21

标签: jquery ajax wcf

我在使用jQuery AJAX从“支持AJAX的WCF服务”中的WebGet函数检索数据时遇到问题。服务代码如下所示:

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

1 个答案:

答案 0 :(得分:0)

所有问题的根源在于,在将名称错误的实体SiteData添加到模型后,我将其重命名为Geofences。如果你这样做,你就会遇到我描述的问题。如果你不这样做,那么一切都很糟糕。

我仍然面临的另一个问题是,当我包含与设计人员识别和实现的SiteData关系的其他实体时,这也会产生序列化错误。手动删除关系会将其分类,但我有最强烈的感觉,您可以通过配置设置修复此问题。谁知道怎么做?

哦,你不能传递alert作为成功函数,传递这样的本机函数似乎解散了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构造函数参数。

我的阴谋理论家怀疑这不是偶然的。实体框架团队不喜欢Linq2Sql,因为它通过一个国家英里执行EF来使他们感到尴尬。

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

相关问题