Search code examples
wcfweb-servicesdatacontract

WCF service multiple method calls one ajax request


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.


Solution

  • 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) ...