4

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;

    }

}
OfirD
  • 9,442
  • 5
  • 47
  • 90
DaneEdw
  • 446
  • 1
  • 8
  • 18

2 Answers2

5

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;


  }

}
DaneEdw
  • 446
  • 1
  • 8
  • 18
  • Is there a way to use WCF services without specifying `DataContract` and `DataMember`, because lot of classes? – Rasool Ghafari Apr 20 '15 at 17:19
  • http://stackoverflow.com/questions/4836683/when-to-use-datacontract-and-datamember-attributes this answer claims that you can, but apparently you lose a lot of benefits when you do so. – DaneEdw Apr 21 '15 at 16:01
  • please check this my question , i want to use `JSON.Net` and `WCF` toghether. http://stackoverflow.com/questions/29754874/how-to-use-wcf-services-without-datacontract-and-datamember – Rasool Ghafari Apr 21 '15 at 20:50
  • 2
    Thank you so much, DaneEdw. I wasted 4 hours trying to find out why my connection was being reset. Your answer saved me. – Jason Williams Oct 19 '16 at 14:13
  • If applying a DataMember to properties, it must contain a `set` for it to serialise (ie, it cannot be readonly). Tip: The set can be blank ;) – Radderz Feb 13 '17 at 14:50
2

I'd like to add a solution related to a case where WCF is used on the server side:

  1. Add diagnostics to web.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>
    
  2. Reproduce the error, then go to the trace folder (C:\Traces, in this example). There will be 2 svclog files there: WCFMessages.svclog and WCFTrace.svclog.

  3. 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, open WCFTrace.svclog, and the errors (in red) would be there.

  • In my case, it was a System.Runtime.Serialization error because of a lack of DataContract attribute.
OfirD
  • 9,442
  • 5
  • 47
  • 90