網站首頁 編程語言 正文
錯誤描述:
由于內部錯誤,服務器無法處理該請求。有關該錯誤的詳細信息,請打開服務器上的 IncludeExceptionDetailInFaults (從 ServiceBehaviorAttribute 或從
配置行為)以便將異常信息發送回客戶端,或打開對每個 Microsoft .NET Framework SDK 文檔的跟蹤并檢查服務器跟蹤日志。
客戶端調用WCF的時候報上面的錯誤,WCF只能序列化基礎的數據類型,不能直接序列化SqlParameter類型,需要使用自定義類,然后在WCF服務端轉換的方式解決:
自定義類代碼如下:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace CommonLib.CustomClass { ////// 方法標記為DataContract約束,屬性標記為DataMember /// [Serializable] [DataContract] public class SetSqlParameter { #region 屬性 ////// 參數名稱 /// [DataMember] private string paraName = ""; public string ParaName { get { return this.paraName; } set { this.paraName = value; } } ////// 參數長度 /// [DataMember] private int paraLength = 0; public int ParaLength { get { return this.paraLength; } set { this.paraLength = value; } } ////// 參數值 /// [DataMember] private object paraValue = null; public object ParaValue { get { return this.paraValue; } set { this.paraValue = value; } } ////// 參數類型 /// [DataMember] private SqlDbType paraDbType = SqlDbType.NVarChar; public SqlDbType ParaDbType { get { return this.paraDbType; } set { this.paraDbType = value; } } #endregion ////// 構造函數 /// /// public SetSqlParameter(SqlParameter sPara) { this.paraName = sPara.ParameterName; this.paraLength = sPara.Size; this.paraValue = sPara.Value; this.paraDbType = sPara.SqlDbType; } ////// 轉換成SqlParameter類型 /// ///public SqlParameter ConvertToSqlParameter() { SqlParameter parameter = new SqlParameter(this.paraName, this.paraDbType, this.paraLength); parameter.Value = this.paraValue; return parameter; } } }
WCF服務端代碼如下:
接口代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Data; using System.Data.SqlClient; using CommonLib.CustomClass; namespace WcfServiceDemo { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“IMyService”。 [ServiceContract] public interface IMyService { [OperationContract] DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters); } }
接口實現類代碼:
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Configuration; using CommonLib.CustomClass; namespace WcfServiceDemo { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼、svc 和配置文件中的類名“MyService”。 // 注意: 為了啟動 WCF 測試客戶端以測試此服務,請在解決方案資源管理器中選擇 MyService.svc 或 MyService.svc.cs,然后開始調試。 public class MyService : IMyService { public DataTable ExeceteQuery(string strSQL, params SetSqlParameter[] parameters) { DataTable dtReturn = new DataTable(); dtReturn.TableName = "ExecuteQuery"; string strCon = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString; using (SqlConnection conn = new SqlConnection(strCon)) { SqlCommand cmd = new SqlCommand(strSQL, conn); conn.Open(); if (parameters != null) { SqlParameter[] para = new SqlParameter[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { //把SetSqlParameter類型的數組轉換成SqlParameter類型的數組 para[i] = parameters[i].ConvertToSqlParameter(); } cmd.Parameters.AddRange(para); } SqlDataAdapter adapter = new SqlDataAdapter(cmd); adapter.Fill(dtReturn); } return dtReturn; } } }
客戶端調用WCF代碼:
using CommonLib.CustomClass; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace winClient { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn_GetData_Click(object sender, EventArgs e) { string strSQL = " SELECT * FROM BaseSetMainInfo WHERE TypeCode=@TypeCode "; //定義SqlParameter SqlParameter para = new SqlParameter("@TypeCode", SqlDbType.Int); para.Value = 1; //定義SetSqlParameter類型的數組 SetSqlParameter[] paras = new SetSqlParameter[] { new SetSqlParameter(para) }; //實例化WCF服務 ServiceReference.MyServiceClient client=new ServiceReference.MyServiceClient(); //調用WCF服務提供的方法 DataTable dt = client.ExeceteQuery(strSQL, paras); this.dataGridView1.DataSource = dt; } } }
這樣就可以解決WCF不能直接序列化SqlParameter類型的問題了。
代碼下載地址:點此下載
原文鏈接:https://www.cnblogs.com/dotnet261010/p/7457527.html
相關推薦
- 2022-07-10 DHCP服務配置——CentOS/Windows2003
- 2022-08-21 caffe的python接口生成solver文件詳解學習_python
- 2024-01-31 nginx配置文件中最后一個 include servers/*;作用是什么?
- 2022-06-22 python?實現?mp3Play?音頻播放_python
- 2022-02-12 redis 快捷啟動 注冊服務,jar包注冊成服務,開機啟動
- 2022-12-25 終于明白tf.reduce_sum()函數和tf.reduce_mean()函數用法_python
- 2022-02-27 微信小程序 - 子組件觸發父組件函數(triggerEvent)
- 2022-07-15 SQL?Server創建用戶定義函數_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支