在WCF中,在客户端如何传List至WCF服务中,谢谢 急

时间:2022-10-30 13:22:31
WCF服务
       /// <summary>
        /// 根据条件取积分兑换商品记录列表并按规定进行排序
        /// </summary>
        /// <param name="oo">ORM操作类</param>
        /// <param name="OrderString">过滤条件</param>
        /// <param name="listParam">过滤条件中的查询参数</param>
        /// <returns>表Info对象集合</returns>
        [OperationContract]
        [FaultContract(typeof(OverflowException))]
        public List<OrderGoodsInfo> OrderGoods_GetList(ListQuery query, List<System.Data.SqlClient.SqlParameter> listParam)
        {
            List<OrderGoodsInfo> list = null;
            return list;
        }

然后客户端用
            CRM.CRMClient client = new CRM.CRMClient();
            client.OperatorCreate();
            CRM.ListQuery query = new CrmTest.CRM.ListQuery()
            {
                Top = 12,
                Filter = "GoodsCode = @GoodsCode AND GoodsName = @GoodsName"
            };
            List<System.Data.SqlClient.SqlParameter> listParam = new List<System.Data.SqlClient.SqlParameter>()
                {
                    new System.Data.SqlClient.SqlParameter("@GoodsCode","123"),
                    new System.Data.SqlClient.SqlParameter("@GoodsName","456"),
                };
            List<CRM.ShopGoodsInfo> list = client.ShopGoods_GetList(query, listParam);
            client.OperatorDispose();
            client.Close();

----------------------------
client.ShopGoods_GetList(query, listParam);
报错:
数据协定名称为“string:http://www.w3.org/2001/XMLSchema”的类型“System.Data.SqlTypes.SqlString”不是所需的类型。请将未知的类型以静态方式添加到已知类型的列表,例如,通过使用 KnownTypeAttribute 属性或通过将其添加到传递给 DataContractSerializer 的已知类型的列表等方法。


谢谢 要怎么办

6 个解决方案

#1


    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    [ServiceKnownType(typeof(StringSqlParam))]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        List<StringSqlParam> getParams();
        // 任务: 在此处添加服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    [KnownType(typeof(StringSqlParam))]
    public class StringSqlParam
    {
        string paramName = "";
        string paramValue = "";

        [DataMember]
        public string ParamName
        {
            get { return paramName; }
            set { paramName = value; }
        }

        [DataMember]
        public string ParamValue
        {
            get { return paramValue; }
            set { paramValue = value; }
        }
    }


仅仅是个思路, 自己搞一个Param 过去再拼装
或者直接用 Dictionary<string, string> getParams();

#2


回楼上的,
public class QueryParameter
    {
        public string Name { get; set; }
        public Type DataType { get; set; }
        public object Value { get; set; }
    }
----------------------
这样,当我给DataType = SqlDbType.BigInt
时,也是报上面的错啊

主要是数据类型如何传进去

谢谢

#3


 

  学习。。。。。。。。。


#4


SqlParameter不可序列化 所以不能传
你的代码里面有 public Type DataType { get; set; } 
Type 也不可序列化所以也不能传

#5


引用 4 楼 lhzyn 的回复:
SqlParameter不可序列化 所以不能传
你的代码里面有 public Type DataType { get; set; }
Type 也不可序列化所以也不能传


对,加一个DataType,但是类型要用 string 或者 int 自己传递,server 和 client 做对应处理
webservices 能传的类型都是基本类型,不能传递的就得尽量自己改造成基本类型进行传输

#6


我跟楼主一样的 问题   怎么在WCF中传递SqlParameter


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;

namespace WcfServices
{
    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        string GetSum(string sql,SqlParameter[] sp);

        // 任务: 在此处添加服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    static class KnownTypesProvider
    {
        static Type[] GetKnownTypes(ICustomAttributeProvider knownTypeAttributeTarget)
        {
            Type contractType = (Type)knownTypeAttributeTarget;
            return contractType.GetGenericArguments();
        }
    }
}

   提示错误信息 
   为找到带有给定消息的命名空间   元数据包含无法解析的引用

#1


    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    [ServiceKnownType(typeof(StringSqlParam))]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        List<StringSqlParam> getParams();
        // 任务: 在此处添加服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    [KnownType(typeof(StringSqlParam))]
    public class StringSqlParam
    {
        string paramName = "";
        string paramValue = "";

        [DataMember]
        public string ParamName
        {
            get { return paramName; }
            set { paramName = value; }
        }

        [DataMember]
        public string ParamValue
        {
            get { return paramValue; }
            set { paramValue = value; }
        }
    }


仅仅是个思路, 自己搞一个Param 过去再拼装
或者直接用 Dictionary<string, string> getParams();

#2


回楼上的,
public class QueryParameter
    {
        public string Name { get; set; }
        public Type DataType { get; set; }
        public object Value { get; set; }
    }
----------------------
这样,当我给DataType = SqlDbType.BigInt
时,也是报上面的错啊

主要是数据类型如何传进去

谢谢

#3


 

  学习。。。。。。。。。


#4


SqlParameter不可序列化 所以不能传
你的代码里面有 public Type DataType { get; set; } 
Type 也不可序列化所以也不能传

#5


引用 4 楼 lhzyn 的回复:
SqlParameter不可序列化 所以不能传
你的代码里面有 public Type DataType { get; set; }
Type 也不可序列化所以也不能传


对,加一个DataType,但是类型要用 string 或者 int 自己传递,server 和 client 做对应处理
webservices 能传的类型都是基本类型,不能传递的就得尽量自己改造成基本类型进行传输

#6


我跟楼主一样的 问题   怎么在WCF中传递SqlParameter


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Reflection;

namespace WcfServices
{
    // 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    [ServiceKnownType("GetKnownTypes", typeof(KnownTypesProvider))]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        string GetSum(string sql,SqlParameter[] sp);

        // 任务: 在此处添加服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    static class KnownTypesProvider
    {
        static Type[] GetKnownTypes(ICustomAttributeProvider knownTypeAttributeTarget)
        {
            Type contractType = (Type)knownTypeAttributeTarget;
            return contractType.GetGenericArguments();
        }
    }
}

   提示错误信息 
   为找到带有给定消息的命名空间   元数据包含无法解析的引用