我有一个通过 WCF 进行通信的客户端-服务器应用程序。我有一个从服务器向客户端发送System.Data.SqlClient.SqlConnection的方法。在工作站上安装 Visual Studio 2012(安装了 .NetFramework 4.5)后,尝试将 SqlConnection 从服务器发送到客户端时,服务器和客户端之间的通信失败。发生该错误的原因是 WCF 服务中使用的 Serializer DataContractSerializer 无法序列化 SqlConnection 对象,因为在 .net framework 4.5 中有一个SqlCredential类型的新属性Credential无法序列化。这是错误:
System.Runtime.Serialization.InvalidDataContractException: Type 'System.Data.SqlClient.SqlCredential' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
在我的解决方案中,我将Target Framework设置为.Net Frameowrk 4.0,甚至将来自 4.0 Framework 的 System.Data.dll 设置为解决方案的参考,以强制应用程序使用这个而不是 GAC 的那个。这样做后在调试模式下看不到 Credential 属性,所以很明显它使用了 System.Data.dll 的 4.0 版本,但是如果我使用反射检查 SqlConnection 类型,则在 SqlConnection 对象中存在 Credential 属性:
var props = typeof(System.Data.SqlClient.SqlConnection).GetProperties();
foreach (var propie in props)
{
string propName = propie.Name;
}
问题是:如何强制我的应用程序在已安装 4.5 .Net Framework 的工作站上使用来自 .net Framework 4.0 的 System.Data.dll?
谢谢你