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:\练习\动态添加TabPage\TabControlDemo\TabControlDemo\Service References\WCFService\Reference.cs:line 165 at TabControlDemo.Form1.tabControl1_SelectedIndexChanged(Object sender, EventArgs e) in d:\练习\动态添加TabPage\TabControlDemo\TabControlDemo\Form1.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 + "\r\n跟踪:" + 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 + "\r\n跟踪:" + 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 + "\r\n跟踪:" + 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 + "\r\n跟踪:" + ex.StackTrace); 66 } 67 return dtReturn; 68 69 }
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决