开发者

WCF json deserialize a byte array exception

I have a WCF service that received a JSON request with a byte array string parameter. Something like:

[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, 
RequestFormat = WebMessageFormat.Json, 
UriTemplate = "images/tag=add", Method = "POST")]
bool UploadImage(byte[] byte);

then, I had a Java client:

FileInputStream fileInputStream = new FileInputStream(filePath);
byte[] imageData = new byte[fileInputStream.available()];
fileInputStream.read(imageData);

then, I sent a JSON string to the server like this:

"byte":[-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,117,0,0,2,-38,8,2,0,0,0,65,61,84,57,0,0,0,1,115,82,71,66,0,-82,-50,28,-23,0,0,0,4,103,65,77,65,0,0,-79,-113,11,-4,97,5,0,0,0,9,112,72,89,115,0,0,14,-61,0,0,14,-61,1,-57,111,-88,100,0,0,-1,-91,73,68,65,84,120,94,-20,-3,9,-101,36,73,114,28,-120,-10,-1,-1,41,-17,45,-33,114,111,-18,46,120,-127,23,72,-128,32,64,-128,-72,65,-100,3,98,-114,-66,-22,62,50,50,-97,-88,-118,-102,-104,-102,-102,-7,17,-111,89,-43,-43,61,57,-97,127,61,81,-111,30,17,30,30,110,-30,-94,-94,-94,-86,95,61,60,-1,-17,-7,12,60,-97,-127,-25,51,-16,105,-50,-64,87,-97,-26,109,-97,-33,-11,-7,12,60,-97,-127,-25,51,-16,-16,-116,47,-49,23,-63,-13,25,120,62,3,-97,-22,12,60,-29,-53,-89,58,-77,-49,-17,-5,124,6,-98,-49,-64,-45,-32,-53,-3,39,-8,-33,-13,111,-13,124,6,-98,-49,-64,-113,-3,12,124,-95,-8,-14,99,63,-83,-49,-57,-1,124,6,-98,-49,0,-50,-64,51,-66,60,95,6,-49,103,-32,-7,12,124,-86,51,112,35,-66,60,58,30,122,-72,-65,-57,118,-10,127,-97,-22,-37,63,-65,-17,-13,25,120,62,3,-97,-14,12,124,90,124,-71,-36,63,-36,-78,77,-56,-13,41,-49,-64,-13,123,63,-97,-127,-25,51,-16,-87,-50,-64,89,124,-39,103,26,-105,-121,-5,-11,118,27,-66,-100,-90,54,-97,-22,-84,60,-65,-17,-13,25,120,62,3,79,113,6,42,-66,-100,-115,88,-58,-3,62,1,-66,92,17,61,-31,88,-98,-30,84,60,-65,-57,-13,25,120,62,3,79,124,6,14,-8,-53,33,-36,108,34,11,25,-51,-83,-4,5,47,116,-127,-26,10,-108,-31,-119,-31,1,63,-15,73,122,126,-69,-25,51,-16,124,6,110,58,3,107,124,-39,-126,21,95,-64,125,19,124,-28,39,-105,59,-36,6,52,122,-85,-83,-105,-73,-29,-84,72,116,-45,-87,120,126,-47,-13,25,120,62,3,79,124,6,-74,-30,-93,1,71,62,17,124,-36,6,58,-7,85,36,43,105,11,-64,121,-30,-109,-12,-4,118,-49,103,-32,-7,12,-36,116,6,58,-66,36,-50,114,12,46,-113,-121,-122,39,121,-121,9,-8,-98,-15,-27,-90,-85,-32,-7,69,-49,103,-32,-45,-100,-127,-81,-90,80,-56,-64,-27,-38,-59,-1,100,4,39,-25,-95,78,28,-58,-4,-71,-97,-26,44,61,-65,-21,-13,25,120,62,3,-73,-100,-127,-118,47,-95,-41,-34,63,-36,93,-42,-37,-75,-48,-13,73,-9,47,7,-7,44,-20,-34,114,9,60,-65,-26,-7,12,124,-78,51,0,124,25,114,52,-124,3,-14,-126,79,10,13,79,-5,-26,60,-32,-25,-1,61,-97,-127,-25,51,-16,69,-99,1,-30,-53,-26,-10,-76,40,-16,41,-34,77,105,-20,47,-22,-76,62,31,-52,-13,25,120,62,3,56,3,29,95,46,-105,7,110,5,110,62,5,40,60,-31,123,-110,127,-99,-4,45,-97,-35,49,39,79,-44,-13,110,-49,103,-32,73,-50,-64,87,92,-22,89,-56,-104,-23,-52,19,-62,-63,-45,-66,-43,85,-32,-126,-13,101,-32,-7,108,-64,123,-110,11,-25,-7,77,-98,-49,-64,-119,51,96,-8,-110,-63,-91,-84,-1,67,-121,-37,-45,-30,-59,-75,-17,86,-104,-117,114,97,91,95,92,95,-106,64,115,-30,-4,60,-17,-14,124,6,-98,-49,-64,-19,103,32,-16,5,97,-47,22,-54,124,-55,90,111,-2,-34,67,-94,29,62,-29,-43,-10,-31,-2,1,27,开发者_运维技巧-65,-87,-53,-40,-49,16,115,-5,-91,-13,-4,-54,-25,51,112,120,6,-66,-78,-107,-26,-101,-16,37,47,66,-3,-43,-10,-39,-56,40,113,-83,-2,32,27,-66,30,97,37,85,42,68,-4,-77,-125,47,-126,-104,107,-61,-85,-61,-77,-7,-68,-61,-13,25,120,62,3,-7,12,24,-66,-28,-115,-32,82,33,-122,-16,-15,-72,122,-59,-49,6,64,-8,58,-122,56,-98,-80,-26,102,48,-28,-30,11,-66,-41,-5,-53,-67,0,-15,89,-117,121,94,12,-49,103,-32,-109,-98,-127,-97,62,-66,-24,-12,17,95,-58,-8,-24,57,68,-6,-92,87,-41,-13,-101,-1,-70,-97,-127,-118,47,-72,-73,23,-14,-46,-61,-97,31,21,127,-55,-28,-123,63,114,-63,23,-89,57,-49,-3,28,126,-35,23,-64,-13,-9,-1,-92,103,-32,10,124,-55,50,-57,103,11,118,110,-8,32,-58,71,-118,-116,102,-2,34,10,19,123,62,-85,-68,-97,-12,18,123,126,-13,95,-29,51,-80,-64,-105,-71,-14,-88,21,13,60,-86,95,-44,13,72,113,-37,75,-126,112,-115,5,3,20,-113,32,-66,20,117,-23,-41,-8,-89,127,-2,-22,-49,103,-32,-109,-97,-127,-97,46,-66,32,-10,73,103,-113,-50,-105,-73,-105,75,-58,23,60,-7,-4,-65,-25,51,-16,124,6,62,-35,25,-8,105,-30,11,41,88,72,48,30,43,1,74,62,-34,-35,23,-2,-30,-69,60,-1,-17,-7,12,60,-97,-127,79,117,6,-10,-16,69,-90,-104,86,81,-3,35,-120,-113,-122,66]"

I received an exception:

System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
   at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.ReadObject(Message message)
   at System.ServiceModel.Dispatcher.SingleBodyParameterMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Did something go wrong?


There are two problems in your code:

  • Some values in the array are negative; byte values go from 0 to 255. You need to use sbyte instead
  • The default body style is Bare, which means that you should send the array directly (no wrapping {"byte":[...]} over the array). You should either change the body style to Wrapped (or WrappedRequest), which is what I did for the code below, or change the client to send the array by itself.

Here's a version of the service which works for the specified JSON

public class StackOverflow_6959064
{
    const string json = "{\"byte\":[-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,117,0,0,2,-38,8,2,0,0,0,65,61,84,57,0,0,0,1,115,82,71,66,0,-82,-50,28,-23,0,0,0,4,103,65,77,65,0,0,-79,-113,11,-4,97,5,0,0,0,9,112,72,89,115,0,0,14,-61,0,0,14,-61,1,-57,111,-88,100,0,0,-1,-91,73,68,65,84,120,94,-20,-3,9,-101,36,73,114,28,-120,-10,-1,-1,41,-17,45,-33,114,111,-18,46,120,-127,23,72,-128,32,64,-128,-72,65,-100,3,98,-114,-66,-22,62,50,50,-97,-88,-118,-102,-104,-102,-102,-7,17,-111,89,-43,-43,61,57,-97,127,61,81,-111,30,17,30,30,110,-30,-94,-94,-94,-86,95,61,60,-1,-17,-7,12,60,-97,-127,-25,51,-16,105,-50,-64,87,-97,-26,109,-97,-33,-11,-7,12,60,-97,-127,-25,51,-16,-16,-116,47,-49,23,-63,-13,25,120,62,3,-97,-22,12,60,-29,-53,-89,58,-77,-49,-17,-5,124,6,-98,-49,-64,-45,-32,-53,-3,39,-8,-33,-13,111,-13,124,6,-98,-49,-64,-113,-3,12,124,-95,-8,-14,99,63,-83,-49,-57,-1,124,6,-98,-49,0,-50,-64,51,-66,60,95,6,-49,103,-32,-7,12,124,-86,51,112,35,-66,60,58,30,122,-72,-65,-57,118,-10,127,-97,-22,-37,63,-65,-17,-13,25,120,62,3,-97,-14,12,124,90,124,-71,-36,63,-36,-78,77,-56,-13,41,-49,-64,-13,123,63,-97,-127,-25,51,-16,-87,-50,-64,89,124,-39,103,26,-105,-121,-5,-11,118,27,-66,-100,-90,54,-97,-22,-84,60,-65,-17,-13,25,120,62,3,79,113,6,42,-66,-100,-115,88,-58,-3,62,1,-66,92,17,61,-31,88,-98,-30,84,60,-65,-57,-13,25,120,62,3,79,124,6,14,-8,-53,33,-36,108,34,11,25,-51,-83,-4,5,47,116,-127,-26,10,-108,-31,-119,-31,1,63,-15,73,122,126,-69,-25,51,-16,124,6,110,58,3,107,124,-39,-126,21,95,-64,125,19,124,-28,39,-105,59,-36,6,52,122,-85,-83,-105,-73,-29,-84,72,116,-45,-87,120,126,-47,-13,25,120,62,3,79,124,6,-74,-30,-93,1,71,62,17,124,-36,6,58,-7,85,36,43,105,11,-64,121,-30,-109,-12,-4,118,-49,103,-32,-7,12,-36,116,6,58,-66,36,-50,114,12,46,-113,-121,-122,39,121,-121,9,-8,-98,-15,-27,-90,-85,-32,-7,69,-49,103,-32,-45,-100,-127,-81,-90,80,-56,-64,-27,-38,-59,-1,100,4,39,-25,-95,78,28,-58,-4,-71,-97,-26,44,61,-65,-21,-13,25,120,62,3,-73,-100,-127,-118,47,-95,-41,-34,63,-36,93,-42,-37,-75,-48,-13,73,-9,47,7,-7,44,-20,-34,114,9,60,-65,-26,-7,12,124,-78,51,0,124,25,114,52,-124,3,-14,-126,79,10,13,79,-5,-26,60,-32,-25,-1,61,-97,-127,-25,51,-16,69,-99,1,-30,-53,-26,-10,-76,40,-16,41,-34,77,105,-20,47,-22,-76,62,31,-52,-13,25,120,62,3,56,3,29,95,46,-105,7,110,5,110,62,5,40,60,-31,123,-110,127,-99,-4,45,-97,-35,49,39,79,-44,-13,110,-49,103,-32,73,-50,-64,87,92,-22,89,-56,-104,-23,-52,19,-62,-63,-45,-66,-43,85,-32,-126,-13,101,-32,-7,108,-64,123,-110,11,-25,-7,77,-98,-49,-64,-119,51,96,-8,-110,-63,-91,-84,-1,67,-121,-37,-45,-30,-59,-75,-17,86,-104,-117,114,97,91,95,92,95,-106,64,115,-30,-4,60,-17,-14,124,6,-98,-49,-64,-19,103,32,-16,5,97,-47,22,-54,124,-55,90,111,-2,-34,67,-94,29,62,-29,-43,-10,-31,-2,1,27,-65,-87,-53,-40,-49,16,115,-5,-91,-13,-4,-54,-25,51,112,120,6,-66,-78,-107,-26,-101,-16,37,47,66,-3,-43,-10,-39,-56,40,113,-83,-2,32,27,-66,30,97,37,85,42,68,-4,-77,-125,47,-126,-104,107,-61,-85,-61,-77,-7,-68,-61,-13,25,120,62,3,-7,12,24,-66,-28,-115,-32,82,33,-122,-16,-15,-72,122,-59,-49,6,64,-8,58,-122,56,-98,-80,-26,102,48,-28,-30,11,-66,-41,-5,-53,-67,0,-15,89,-117,121,94,12,-49,103,-32,-109,-98,-127,-97,62,-66,-24,-12,17,95,-58,-8,-24,57,68,-6,-92,87,-41,-13,-101,-1,-70,-97,-127,-118,47,-72,-73,23,-14,-46,-61,-97,31,21,127,-55,-28,-123,63,114,-63,23,-89,57,-49,-3,28,126,-35,23,-64,-13,-9,-1,-92,103,-32,10,124,-55,50,-57,103,11,118,110,-8,32,-58,71,-118,-116,102,-2,34,10,19,123,62,-85,-68,-97,-12,18,123,126,-13,95,-29,51,-80,-64,-105,-71,-14,-88,21,13,60,-86,95,-44,13,72,113,-37,75,-126,112,-115,5,3,20,-113,32,-66,20,117,-23,-41,-8,-89,127,-2,-22,-49,103,-32,-109,-97,-127,-97,46,-66,32,-10,73,103,-113,-50,-105,-73,-105,75,-58,23,60,-7,-4,-65,-25,51,-16,124,6,62,-35,25,-8,105,-30,11,41,88,72,48,30,43,1,74,62,-34,-35,23,-2,-30,-69,60,-1,-17,-7,12,60,-97,-127,79,117,6,-10,-16,69,-90,-104,86,81,-3,35,-120,-113,-122,66]}";
    [ServiceContract]
    public class Service
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            UriTemplate = "images/tag=add",
            Method = "POST",
            BodyStyle = WebMessageBodyStyle.Wrapped)]
        public bool UploadImage(sbyte[] @byte)
        {
            Console.WriteLine("Length: {0}", @byte.Length);
            return true;
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        c.Headers[HttpRequestHeader.ContentType] = "application/json";
        Console.WriteLine(c.UploadString(baseAddress + "/images/tag=add", json));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}


It worked for short json string. But I tried to send a longer string then Wcf cant deserialize json string although I added

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="25000000" />
  </webServices>
</scripting>

and

<httpRuntime executionTimeout="240" maxRequestLength="20480" />

So what should i do for example this json string http://www.mediafire.com/?323h2kc69ykm200

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜