开发者

How do I prevent WCF from auto-serializing byte-array as base-64 encoded string?

开发者 https://www.devze.com 2023-01-21 02:32 出处:网络
When invoking a service, my client code passes in data. One of the data members is a by开发者_JAVA技巧te array, so WCF will automatically encode this into a base-64 string during serialization. The pr

When invoking a service, my client code passes in data. One of the data members is a by开发者_JAVA技巧te array, so WCF will automatically encode this into a base-64 string during serialization. The problem is, the data contract of the service is expecting a byte array, not a string, so deserialization of the data will result in an exception: Expecting state 'Element'.. Encountered 'Text' with name '', namespace ''

How do I work around this glitch?


I'm not sure I understand what you mean? What does the contract for your service look like? I very much doubt the problem is the base-64 serialization, unless your service is expecting the binary data encoded in hexbinary format instead.

Remember, you're using XML here, so binary data can't travel unencoded; it needs to be serialized into a text format that can be embedded in the SOAP envelope (unless you're using MTOM), and that's usually Base-64.


It can be caused by a mismatch between client and service. Did you try refreshing your client proxy (by updating the service reference for example)?


My solution was to manually alter the autogenerated data contracts from byte[] to int[]. The XML now passes validation because each element of the int array is put inside a separate element.

The drawback is having to manually alter the data contracts if you regenerate the files from WSDLs again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号