WCF Rest ERR_CONNECTION_RESET响应不大

时间:2014-10-22 18:07:01

标签: wcf

错误代码非常糟糕,ERR_CONNECTION_RESET有很多原因,我在其他问题上找到的原因与大型Web服务调用的MaxRequestLength太小有关。我返回的数据只有几个KB,所以这不是问题。

这是我的界面代码

[WebGet(RequestFormat = WebMessageFormat.Json,
  BodyStyle = WebMessageBodyStyle.WrappedRequest,
  ResponseFormat = WebMessageFormat.Json,
  UriTemplate = "GetReportByID?ReportID={ReportID}")]
[OperationContract]
UsageReport GetReportByID(int ReportID);

这是实施

public UsageReport GetReportByID(int ReportID)
{
    return new UsageReport(ReportID);
}

这是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;

    }

}

2 个答案:

答案 0 :(得分:5)

问题是我的UsageItem类,我缺少必要的DataContract和DataMember字段。

[DataContract]
public class UsageItem
{
[DataMember]
int ItemNumber;

public UsageItem(int ItemNumber)
  {
    this.ItemNumber = ItemNumber;


  }

}

答案 1 :(得分:0)

我想添加两件事:

  1. 对我来说,Chrome中的等效错误是ERR_CONNECTION_RESET,所以我查了一下,发现了this answer,就是这样。

  2. 与在服务器端使用WCF的情况有关的解决方案:

    a。将diagnostics添加到web.config(取自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>
    

    b。重现该错误,然后转到跟踪文件夹(在本示例中为C:\Traces),然后单击WCFMessages.svclog文件。将打开一个“ Microsoft Service Trace Viewer”窗口,显示红色错误。在我的情况下,这是一个System.Runtime.Serialization错误,因为缺少DataContract属性。