由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribute 或从 <serviceDebug> 配置行为)以便将异常信息发送回客户端,或打开对每个 Microsoft .NET Framework SDK 文档的跟踪并检查服务器跟踪日志。
2024-08-27 18:32:13
客户端调用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
{
/// <summary>
/// 方法标记为DataContract约束,属性标记为DataMember
/// </summary>
[Serializable]
[DataContract]
public class SetSqlParameter
{
#region 属性 /// <summary>
/// 参数名称
/// </summary>
[DataMember]
private string paraName = "";
public string ParaName
{
get { return this.paraName; }
set { this.paraName = value; } } /// <summary>
/// 参数长度
/// </summary>
[DataMember]
private int paraLength = ;
public int ParaLength
{ get { return this.paraLength; }
set { this.paraLength = value; }
} /// <summary>
/// 参数值
/// </summary>
[DataMember]
private object paraValue = null;
public object ParaValue
{
get { return this.paraValue; }
set { this.paraValue = value; }
} /// <summary>
/// 参数类型
/// </summary>
[DataMember]
private SqlDbType paraDbType = SqlDbType.NVarChar;
public SqlDbType ParaDbType
{
get { return this.paraDbType; } set { this.paraDbType = value; }
} #endregion /// <summary>
/// 构造函数
/// </summary>
/// <param name="sPara"></param>
public SetSqlParameter(SqlParameter sPara)
{
this.paraName = sPara.ParameterName;
this.paraLength = sPara.Size;
this.paraValue = sPara.Value;
this.paraDbType = sPara.SqlDbType;
} /// <summary>
/// 转换成SqlParameter类型
/// </summary>
/// <returns></returns>
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 = ; 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 = ; //定义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://files.cnblogs.com/files/dotnet261010/WcfSqlParameterDemo.rar
最新文章
- 我的基于asp.net mvc5 +mysql+dapper+easyui 的Web开发框架(0)
- Centos7安装Mono(以4.6.0)为例
- JavaScript 闭包系列二(匿名函数及函数的闭包)
- 正确的选择log级别
- [Architect] ABP(现代ASP.NET样板开发框架) 翻译
- POP3,全名为“Post Office Protocol - Version 3”,即“邮局协议版本3”
- nodejs-fs使用
- web标准(复习)--2 列布局
- Xcode7 使用NSURLSession发送HTTP请求报错
- <;meta>;标签的作用
- ecshop订单状态对应值详解
- 【UML 建模】状态图介绍
- react安装 项目构建
- Python基础-数据类型-转摘
- JavaWeb基础之Servlet简单实现用户登陆
- 关于 python中的转义字符
- Code once, debug everywhere.
- JS报错修改日记(1):Uncaught ReferenceError: showQRcode is not defined
- C#、AE开发入门之打开CAD文件并显示
- 基于开源SuperSocket实现客户端和服务端通信项目实战
热门文章
- /etc/vsftpd.conf配置(ftp上传)
- 在低带宽或不可靠的网络环境中安装 Visual Studio 2017
- [Grunt] Watch &;&; grunt-contrib-watch
- 清除tomcat的缓存
- WCF学习笔记之并发与限流
- 金蝶K3,域环境中,无本地用户管理员权限的域用户如何设置注册表权限?
- Patterns-Proxy
- Android中源码Launcher主屏幕程序排列详解【安卓Launcher进化一】
- EMQ ---websocket
- C#基础—不安全代码(unsafe code)