WCF REST服务的非常奇怪的行为

时间:2014-01-14 09:47:00

标签: .net wcf rest responseformat

我设计了一个带有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 respone启用List retrun类型的东西吗?

0 个答案:

没有答案