0

我设计了一个带有 Json 响应的 REST 服务。直到昨天它工作正常。即使现在,如果我将 ResponseFormat = WebMessageFormat.Json 更改为 ResponseFormat = WebMessageFormat.Xml,它仍然有效。任何人都可以建议,为什么当我将 ResponseFormat 设置为 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);

相同的客户端使用 Xml 响应,但它会因 Json 响应而失败,并出现以下错误。我开始这个几周了,它一直工作到昨天。:(

    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: 

我进一步分析发现,如果我将返回类型从 List 更改为某种类型,例如字符串。它工作正常。我是否缺少为我的 Json 响应启用 List 重新运行类型的内容?

4

0 回答 0