I have spent most of the day searhing for an answer to my question, but have not found anything that solves my problem. I hope some of you can help me out.
I am implementing a WCF service which will accept calls from a third party application. The third pary app has a fixed XML it sends for each of the methods I am required to handle. All simple methods are working, except one where the method accepts an array of custom types as parameter.
My method definition looks like this:
[OperationContract]
[XmlSerializerFormat(Style = OperationFormatStyle.Document, SupportFaults = true, Use = OperationFormatUse.Literal)]
public bool NewDataDeviceRecords(DeviceRecord[] deviceRecords);
By creating a sample project and referencing my service I have verified that most of the XML looks correct. The only problem is the part concerning the 'deviceRecords' parameter.
When the third party app posts, the XML looks like this (for method parameter(s) only!):
When I post with my test client, I get the following (naturally)
The result is that the service fails to parse the
I have tried using [MessageParameter(Name = "param-1")] in front of the method parameter, but this simply results in the following:
It simply ignores the dash! Very frustrating!! I have tried HTML encoding and URL encoding without luck. I then decided to try and wrap the static array in a wrapper class DeviceRecordArray which simply holds an array of DeviceRecord objects. I then used the [DataContract(Name = "param-1")] on the class, and [XmlElement("DeviceRecord")] on the contained array. The method signature is now changed to this:
public bool NewDataDeviceRecords(DeviceRecordArray deviceRecords);
This does not solve my problem though. It gives me the correct XML structure now, but it is still encapsulated in an extra (outer) element:
Is there a way to either force the MessageParameterAttribute to accept my dash, or can I use the latter option and somehow remove the extra (outer) element 'deviceRecords'?
Any help is much appreciated!!
/Nicolai
Nicolai HenriksenPosted Nov 26, 2012, 2:47 AM
I have now tried "escaping" the parameter name with single quotes. Unfortunately this gives me the same result (i.e. "param-1" is changed to "param1" and XML deserialization fails).
Suthish:
I already posted the code, but here it is again with the MessageParameter applied.
public bool NewDataDeviceRecords([MessageParameter(Name = "'param-1'")] DeviceRecord[] deviceRecords)
...
}
Unless some clever person out there can come up with the solution, I think I will have to revert back to simply handling the posted XML on my own. By that I mean modifying my method signature to the signature below, and extracting the desired XML from the message and then deserialize it to my object collection.
public Message NewDataDeviceRecords(Message input)
{
string xml = input.GetReaderAtBodyContents().ReadInnerXml();
...
}
Thanks for your help so far... I hope there is someone out there who can come up with the right solution :-)
/Nicolai
Suthish NairPosted Nov 24, 2012, 10:28 AM
VulpesPosted Nov 23, 2012, 2:49 PM
[MessageParameter(Name = "'param-1'")]
Nicolai HenriksenPosted Nov 23, 2012, 12:03 PM
I know that it is simple to get rid of that way. But it all goes on behind the scenes! It's the deserialization of the received XML that does not parse correctly to my custom type (DeviceRecord array), and the reason for this is simply that the default serializer expects an element named "deviceRecords" but finds "param-1". I can then attempt to force the deserializer to look for "param-1" using the MessageParameter attribute, however, it seems that dashes at not allowed, and instead of searching for "param-1" it searches for "param1".
I can get around this by changing my method signature to take a Message instance as parameter and likewise return a Message instance. I can then handle the XML deserialization by myself. But I find it hard to believe that this is the only solution to the problem, and I would much rather have a type-safe method signature than one which takes Message object which could contain whatever.
I hope the above makes sense!
/Nicolai
VulpesPosted Nov 23, 2012, 10:43 AM
Nicolai HenriksenPosted Nov 23, 2012, 10:05 AM
Suthish NairPosted Nov 23, 2012, 8:44 AM
Same logic needs to apply for your method.
http://stackoverflow.com/questions/2720617/how-to-get-all-methods-from-wcf-service