问题
The error code is absolutely terrible, ERR_CONNECTION_RESET has a host of causes and the causes that I found on other questions were related to having too small of a MaxRequestLength for large web service calls. The data I was returning was only a couple of kB though, so this couldn't be the issue.
Here is my interface code
[WebGet(RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetReportByID?ReportID={ReportID}")]
[OperationContract]
UsageReport GetReportByID(int ReportID);
This was the implementation
public UsageReport GetReportByID(int ReportID)
{
return new UsageReport(ReportID);
}
And this was the class code for UsageReport
[DataContract]
public class UsageReport
{
[DataMember]
List<UsageItem> RL;
public UsageReport(int reportID)
{
RL = new List<UsageItem>();
using (SqlDataReader dr = DBUtility.ExecuteReader(cmd, "DBString"))
{
while (dr.Read())
{
ItemNumber = dr["ItemID"] as int? ?? 0;
RL.Add(new UsageItem(ItemNumber));
}
dr.Close();
}
}
public class UsageItem
{
int ItemNumber;
public UsageItem(int ItemNumber)
{
this.ItemNumber = ItemNumber;
}
}
回答1:
The problem was my UsageItem class, I was missing the necessary DataContract and DataMember fields.
[DataContract]
public class UsageItem
{
[DataMember]
int ItemNumber;
public UsageItem(int ItemNumber)
{
this.ItemNumber = ItemNumber;
}
}
回答2:
I'd like to add a solution related to a case where WCF is used on the server side:
Add
diagnostics
toweb.config
(taken from here):<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.diagnostics> <sources> <source name="System.Net" switchValue="Verbose"> <listeners> <add name="SystemNetTrace"/> </listeners> </source> <source name="System.ServiceModel" switchValue="Verbose, ActivityTracing" propagateActivity="true"> <listeners> <add name="wcftrace" /> </listeners> </source> <source name="System.ServiceModel.MessageLogging" switchValue="Verbose, ActivityTracing"> <listeners> <add name="wcfmessages" /> </listeners> </source> <source name="System.Runtime.Serialization" switchValue="Verbose"> <listeners> <add name="wcfmessages" /> </listeners> </source> </sources> <sharedListeners> <add name="SystemNetTrace" type="System.Diagnostics.TextWriterTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, Callstack" initializeData="C:\Traces\System_Net.txt" /> <add name="wcftrace" type="System.Diagnostics.XmlWriterTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, Callstack" initializeData="C:\Traces\WCFTrace.svclog" /> <add name="wcfmessages" type="System.Diagnostics.XmlWriterTraceListener" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, Callstack" initializeData="C:\Traces\WCFMessages.svclog" /> </sharedListeners> <trace autoflush="true" /> </system.diagnostics> </configuration>
Reproduce the error, then go to the trace folder (
C:\Traces
, in this example). There will be 2svclog
files there:WCFMessages.svclog
andWCFTrace.svclog
.Open the file named
WCFMessages.svclog
. A "Microsoft Service Trace Viewer" window will open, showing errors with a red color.
If no errors are displayed, openWCFTrace.svclog
, and the errors (in red) would be there.
- In my case, it was a
System.Runtime.Serialization
error because of a lack ofDataContract
attribute.
来源:https://stackoverflow.com/questions/26514156/wcf-rest-err-connection-reset-not-large-response