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         }

 

转载于:https://www.cnblogs.com/dotnet261010/p/7056498.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值