WCF服务多个方法调用一个ajax请求

发布于 2024-11-27 04:34:31 字数 489 浏览 5 评论 0原文

在开始我的问题之前,先介绍一些背景信息。我有通过 jquery.ajax 调用的 WCF 服务。我可以在控制台窗口中看到并使用其他工具验证该请求仅发生一次。

在服务器端,我看到该方法准确地触发了 3 次。怎么会发生这种事?在服务上,我使用以下属性和签名进行装饰

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public ResponseEnvelope AddToCart(Order[] Order)  //ResponseEnvelope
{
   .....
}

有趣的是,当我将响应从 ResponseEnvelope 更改为 void 时,它工作正常。

我怀疑这可能与我的 DataContracts 和对象层次结构有关,我现在正在测试它。

任何帮助表示赞赏。

Before I begin my question a little background info. I have the WCF service that is being called via jquery.ajax. I can see in the console window and verify with other tools that the request is happening only once.

On the server side, I see the method firing 3 times exactly. How could this happen? On the service I am decorating with the following attributes and signature

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public ResponseEnvelope AddToCart(Order[] Order)  //ResponseEnvelope
{
   .....
}

The interesting thing is that when I change the response from ResponseEnvelope to void it works fine.

I am suspecting this might have something to do with my DataContracts and object hierarchy and I am testing through that now.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-12-04 04:34:31

我一直在度假,但这是这个问题的答案,以防它对任何人有帮助。该问题可能没有包含足够的信息来解决问题。

事实证明问题出在序列化器内部。在 DataContract 中使用对象作为类型会产生一个错误,我最终能够捕获该错误。该错误消息实际上非常明确地说明了如何解决该问题,因此我将就此保留。

将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。
在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract、XmlWriterDelegator xmlWriter、对象 obj、布尔 verifyKnownType、RuntimeTypeHandle 声明的 TypeHandle)
在 System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiType(XmlWriterDelegator xmlWriter,对象 obj,RuntimeTypeHandle objectTypeHandle,类型 objectType,Int32 声明类型 ID,RuntimeTypeHandle 声明类型句柄,类型声明类型)
...

I've been on vacation but here is the answer to this problem in case it helps anyone down the line. The question likely didn't contain enough information to solve the problem.

As it turns out the problem is in inside the serializer. Using object as a type within the DataContract generated an error that I was eventually able to capture. The error message was actually very explicit about how to solve the issue so I will leave it at that.

Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle)
at System.Runtime.Serialization.Json.XmlObjectSerializerWriteContextComplexJson.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文