【问题标题】:WCF Rest Service is called TwiceWCF 休息服务被称为两次
【发布时间】:2012-11-12 09:45:53
【问题描述】:

我的 WCF 休息服务调用了两次我不知道确切的原因。当使用客户端应用程序进行调试时,它会被调用两次。谁能指出我出了什么问题...

服务声明:

    [ServiceContract]
        public interface IVoteCountService
        {
               [OperationContract]
            [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat =                 WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "/GetCandidate")]
            CandisteListResponse CandidateList();
}

[DataContract]
    public class CandisteListResponse
    {
        public CandidateList CandidateList { get;set;}
    }

服务定义:

public CandisteListResponse CandidateList()
        {
            CandidateList objVoterList = new CandidateList();
            objVoterList = CandidateListDataService.GetCandidateList();

            if (objVoterList.Count > 0)
            {

                return new CandisteListResponse {  CandidateList = objVoterList };
            }
            else
            {

                return new CandisteListResponse { CandidateList = objVoterList };
            }
        }

CandidateListDataService.cs:

public class CandidateListDataService
    {
        public static CandidateList GetCandidateList()
        {
            CandidateList obj = new CandidateList();
            using (SqlConnection MyConnection = new SqlConnection(SqlServerDataSource.GetConnectionString()))
                try
                {
                    if (ConfigurationManager.AppSettings["SelectDB"] == "SQLServer")
                    {
                        {
                            SqlCommand MyCommand = new SqlCommand("usp_service_get_candidate", MyConnection);
                            MyCommand.CommandType = CommandType.StoredProcedure;
                            MyCommand.CommandTimeout = 56000;

                            MyConnection.Open();

                            using (SqlDataReader MyReader = MyCommand.ExecuteReader())
                            {

                                while (MyReader.Read())
                                {
                                    obj.Add(FillVoterInformation(MyReader));


                                }

                                MyReader.Close();
                                MyReader.Dispose();
                            }

                        }
                    }

                }
                catch (Exception ex)
                {
                    MyConnection.Dispose();

                }
                finally
                {
                    MyConnection.Dispose();

                }
            return obj;

        }


        private static Candidate FillVoterInformation(SqlDataReader MyDataRecord)
        {
            Candidate MyVoterlist = new Candidate();

            try
            {
                MyVoterlist.CODE = Convert.ToString(MyDataRecord["CODE"]);
                MyVoterlist.CNAME = Convert.ToString(MyDataRecord["CNAME"]);
                MyVoterlist.SH_NAM = Convert.ToString(MyDataRecord["SH_NAM"]);

                MyVoterlist.TV = (MyDataRecord["TV"] == DBNull.Value ? Convert.ToInt32(0) : Convert.ToInt32(MyDataRecord["TV"]));

                MyVoterlist.TVCNT = (MyDataRecord["TVCNT"] == DBNull.Value ? Convert.ToInt32(0) : Convert.ToInt32(MyDataRecord["TVCNT"]));
                MyVoterlist.ROW_NO = (MyDataRecord["ROW_NO"] == DBNull.Value ? Convert.ToInt32(0) : Convert.ToInt32(MyDataRecord["ROW_NO"]));

            }
            catch (Exception ex)
            {
                throw ex;

            }
            return MyVoterlist;

        }


    }

网页配置:

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="streamWebHttpbinding" transferMode="Streamed"  maxReceivedMessageSize="1000000000000" receiveTimeout="01:00:00" sendTimeout="01:00:00" />
      </webHttpBinding>

    </bindings>
    <services>
      <service name="VoteCountService.VoteCountWCFService" behaviorConfiguration="ServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding"  contract="VoteCountService.IVoteCountService" behaviorConfiguration="web" bindingConfiguration="streamWebHttpbinding" >
        </endpoint>
      </service>
      </services>
    <behaviors>
      <serviceBehaviors>
         <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
          <dataContractSerializer maxItemsInObjectGraph="10000000"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

提前致谢

维杰

【问题讨论】:

    标签: http-get wcf-rest


    【解决方案1】:

    在我的应用程序中,我使用了 Dataetime 转换,如下所示

     private static Voter FillVoterInformation(SqlDataReader MyDataRecord)
                {
                    Voter MyVoterlist = new Voter();
    
                    try
                    {
                      MyVoterlist.VOTERDOB = (MyDataRecord["VOTERDOB"] == DBNull.Value? Convert.ToDateTime(null): Convert.ToDateTime(MyDataRecord["VOTERDOB"]));
        }
        catch (Exception ex)
                    {
                        throw ex;
    
                    }
                    return MyVoterlist;
    
                }
    
    When I had analyzed TraceListner I Found that the DateTime Conversion Throws the following  following Exception.
    
    "DateTime values that are greater than DateTime.MaxValue or smaller than DateTime.MinValue when converted to UTC cannot be serialized to JSON." 
    
    I have Solved this issue by converting DateTime to UTC Time Following is the Code  
    
    item.VOTERDOB = DateTime.SpecifyKind(item.VOTERDOB, DateTimeKind.Utc);
    

    谢谢

    维杰

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 2013-10-19
      相关资源
      最近更新 更多