0

I have designed a REST service with Json response. It was working fine till yesterday. Even now, it works if I change ResponseFormat = WebMessageFormat.Json to ResponseFormat = WebMessageFormat.Xml. Can anyone advise, why as of sudden i'm not abel to get response when I set ResponseFormat to Json?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/Test.svc/GetData?Id=1");
        WebResponse response = request.GetResponse();
        string result = new StreamReader(response.GetResponseStream()).ReadToEnd();
        Console.WriteLine(result);

The same client works with Xml response but it fails with Json response with the below error. I started this a couple of weeks and it was working till yesterday. :(

    System.Net.WebException was unhandled
  HResult=-2146233079
  Message=The underlying connection was closed: An unexpected error occurred on a receive.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()
       at ConsoleTest.Program.Main(String[] args) in c:\Sarath\dotNet Samples\ConsoleTest\ConsoleTest\Program.cs:line 28
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.IOException
       HResult=-2146232800
       Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
       Source=System
       StackTrace:
            at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
       InnerException: System.Net.Sockets.SocketException
            HResult=-2147467259
            Message=An existing connection was forcibly closed by the remote host
            Source=System
            ErrorCode=10054
            NativeErrorCode=10054
            StackTrace:
                 at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                 at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
            InnerException: 

I have further analyzed and found that if I change my return type from List to some type like string. It works fine. Am I missing something to enable List retrun type for my Json respone?

techspider
  • 3,370
  • 13
  • 37
  • 61
  • This looks like error on the client (caller) side and it doesn't tell much, except the service closed a connection. There should be another exception on service's side. It may be hard to get. I suggest to implement the IErrorHandler (find in internet how exactly). Also it may be configuration issue, if 'the same code' worked before. – Kamarey Jan 14 '14 at 09:57
  • I have further analyzed and found that if I change my return type from List to some type like string. It works fine. Am I missing something to enable List retrun type for my Json respone? Even I tried placing myclass as return type instead of List, it doesn't work. If change response format to Xml, anything works. – techspider Jan 14 '14 at 09:58
  • The resolution is mind-blowing. I see one of the data contract fields is NULL in the DB but it is not Nullable type in C#. It is resulting in the strange behavior my REST Service. – techspider Jan 14 '14 at 10:20
  • Is it a duplicate of your own similar question: http://stackoverflow.com/questions/21093979/strange-error-with-wcf-rest-services ? – Pranav Singh Jan 14 '14 at 11:13
  • unfortunately, yes, as the errors originated were different, i expected to be different issues. But, at the end, both resolve to the same fix. – techspider Jan 15 '14 at 13:28

0 Answers0