zoukankan      html  css  js  c++  java
  • WCF客户端获取服务器返回数据报错

    错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExecuteSQL.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

    跟踪堆栈信息:

    Server stack trace:    at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)    at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)    at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)    at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

    Exception rethrown at [0]:    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)    at TabControlDemo.WCFService.IPubExecuteSQL.GetDataTableByProcedure(String procName, String[] parameterValues)    at TabControlDemo.WCFService.PubExecuteSQLClient.GetDataTableByProcedure(String procName, String[] parameterValues) in d:练习动态添加TabPageTabControlDemoTabControlDemoService ReferencesWCFServiceReference.cs:line 165    at TabControlDemo.Form1.tabControl1_SelectedIndexChanged(Object sender, EventArgs e) in d:练习动态添加TabPageTabControlDemoTabControlDemoForm1.cs:line 119

    错误原因:

     返回DataTable时没有TableName,导致不能序列化。

    原代码:

     1 /// <summary>
     2         /// 调用存储过程返回DataTable
     3         /// </summary>
     4         /// <param name="procName">存储过程名称</param>
     5         /// <param name="parameterValue">存储过程参数值</param>
     6         /// <returns></returns>
     7         public DataTable GetDataTableByProcedure(string procName, string[] parameterValues)
     8         {
     9             DataTable dtReturn = new DataTable();
    10             try
    11             {
    12                 //连接字符串
    13                 string strConn = "";
    14                 try
    15                 {
    16                     string sFilePath = HttpRuntime.AppDomainAppPath + "..\Connect.config";
    17                     if (System.IO.File.Exists(sFilePath))
    18                     {
    19                         ExeConfigurationFileMap file = new ExeConfigurationFileMap();
    20                         file.ExeConfigFilename = sFilePath;
    21                         Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
    22                         strConn = config.ConnectionStrings.ConnectionStrings["HealthHospInfection"].ToString();
    23                     }
    24                     else
    25                     {
    26                         strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ToString();
    27                     }
    28                 }
    29                 catch (Exception ex)
    30                 {
    31                     strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ToString();
    32                     SILogUtil.Error("获取连接字符串错误:" + ex.Message + "
    跟踪:" + ex.StackTrace);
    33                 }
    34 
    35                 SqlConnection conn = new SqlConnection(strConn);
    36                 SqlCommand cmd = new SqlCommand();
    37                 cmd.Connection = conn;
    38                 cmd.CommandType = CommandType.StoredProcedure;
    39                 cmd.CommandText = procName;
    40                 conn.Open();
    41 
    42                 //获取存储过程的参数
    43                 SqlCommandBuilder.DeriveParameters(cmd);
    44                 //移除存储过程参数
    45                 cmd.Parameters.RemoveAt(0);
    46 
    47                 //设置参数值
    48                 if (parameterValues != null)
    49                 {
    50                     for (int i = 0; i < cmd.Parameters.Count; i++)
    51                     {
    52                         cmd.Parameters[i].Value = parameterValues[i];
    53                     }
    54                 }
    55 
    56                 SqlDataAdapter adapter = new SqlDataAdapter();
    57                 adapter.SelectCommand = cmd;
    58                 //填充数据
    59                 adapter.Fill(dtReturn);
    60             }
    61             catch (Exception ex)
    62             {
    63                 SILogUtil.Error("通过Proc获取数据出错:" + ex.Message + "
    跟踪:" + ex.StackTrace);
    64             }
    65             return dtReturn;
    66 
    67         }

    修改之后的代码:

     1 /// <summary>
     2         /// 调用存储过程返回DataTable
     3         /// </summary>
     4         /// <param name="procName">存储过程名称</param>
     5         /// <param name="parameterValue">存储过程参数值</param>
     6         /// <returns></returns>
     7         public DataTable GetDataTableByProcedure(string procName, string[] parameterValues)
     8         {
     9             DataTable dtReturn = new DataTable();
    10             //设置TableName
    11             dtReturn.TableName = "ExecuteNoQuery";
    12             try
    13             {
    14                 //连接字符串
    15                 string strConn = "";
    16                 try
    17                 {
    18                     string sFilePath = HttpRuntime.AppDomainAppPath + "..\Connect.config";
    19                     if (System.IO.File.Exists(sFilePath))
    20                     {
    21                         ExeConfigurationFileMap file = new ExeConfigurationFileMap();
    22                         file.ExeConfigFilename = sFilePath;
    23                         Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
    24                         strConn = config.ConnectionStrings.ConnectionStrings["HealthHospInfection"].ToString();
    25                     }
    26                     else
    27                     {
    28                         strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ToString();
    29                     }
    30                 }
    31                 catch (Exception ex)
    32                 {
    33                     strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ToString();
    34                     SILogUtil.Error("获取连接字符串错误:" + ex.Message + "
    跟踪:" + ex.StackTrace);
    35                 }
    36 
    37                 SqlConnection conn = new SqlConnection(strConn);
    38                 SqlCommand cmd = new SqlCommand();
    39                 cmd.Connection = conn;
    40                 cmd.CommandType = CommandType.StoredProcedure;
    41                 cmd.CommandText = procName;
    42                 conn.Open();
    43 
    44                 //获取存储过程的参数
    45                 SqlCommandBuilder.DeriveParameters(cmd);
    46                 //移除存储过程参数
    47                 cmd.Parameters.RemoveAt(0);
    48 
    49                 //设置参数值
    50                 if (parameterValues != null)
    51                 {
    52                     for (int i = 0; i < cmd.Parameters.Count; i++)
    53                     {
    54                         cmd.Parameters[i].Value = parameterValues[i];
    55                     }
    56                 }
    57 
    58                 SqlDataAdapter adapter = new SqlDataAdapter();
    59                 adapter.SelectCommand = cmd;
    60                 //填充数据
    61                 adapter.Fill(dtReturn);
    62             }
    63             catch (Exception ex)
    64             {
    65                 SILogUtil.Error("通过Proc获取数据出错:" + ex.Message + "
    跟踪:" + ex.StackTrace);
    66             }
    67             return dtReturn;
    68 
    69         }
  • 相关阅读:
    cvCreateStructuringElementEx理解
    GNU_GSL相关
    粒子滤波(转)
    C++指针拷贝
    c++中的复制构造函数
    通过几道题目找自信
    C++网络编程基础
    linux system : install flash player
    ContentType一览
    O_RDWR O_CREAT等open函数标志位在哪里定义?(格式还要编译,答案在最后一段)
  • 原文地址:https://www.cnblogs.com/dotnet261010/p/7056498.html
Copyright © 2011-2022 走看看