none
自定义一个类,在wcf中调用时,方法参数用到这个类,为什么就报“超时之类的错误”,只要去掉这个类就可以正常运行。 RRS feed

  • 问题

  • [DataContract]
      public class DataInfo
      {
        [DataMember]
        public string SQL { get; set; }
        [DataMember]
        public SqlParameter[] paras { get; set; }
        
      }
    


    自定义一个类,在wcf中调用时,方法参数用到这个类,为什么就报“超时之类的错误”,只要去掉这个类就可以正常运行。

     try
          {
            ServiceReference4.DataInfo datainfo = new ServiceReference4.DataInfo();
            ServiceReference4.JGLReturnData ret = new ServiceReference4.JGLReturnData();
            ServiceReference4.ServicesClient host = new ServiceReference4.ServicesClient();
            ServiceReference4.SqlHelper obj = new ServiceReference4.SqlHelper();
            string strHospitalId, strName, strAddr, strIsPartner, strRemark;
             strHospitalId="100";
             strName = "100"; 
            strAddr = "100"; 
            strIsPartner = "100";
            strRemark = "100";
            string strSql = @"insert into base_hospital (Hospitalid,name,addr,IsPartner,remark) 
                      values (@HospitalId,@Name,@Addr,@IsPartner,@Remark)";
            SqlParameter[] sqlPars = 
          {
            new SqlParameter("@HospitalId",SqlDbType.NChar),
            new SqlParameter("@Name",SqlDbType.NChar),
            new SqlParameter("@Addr",SqlDbType.NChar),
            new SqlParameter("@IsPartner",SqlDbType.NChar),
            new SqlParameter("@Remark",SqlDbType.NChar)
          };
    
            sqlPars[0].Value = strHospitalId;
            sqlPars[1].Value = strName;
            sqlPars[2].Value = strAddr;
            sqlPars[3].Value = strIsPartner;
            sqlPars[4].Value = strRemark;
    
            datainfo.SQL = strSql;
            datainfo.paras = sqlPars;
    ////这样写把datainfo这个添加进去,就会报超时之类的错误
            ret = host.CreateFactory(obj, "Insert", datainfo);
    ///这样写就可以正常运行,不会报超时的错误,
    //ret = host.CreateFactory(obj, "Insert", datainfo);
    
            if (ret.Level_DataMember == 0)
            {
              MessageBox.Show("成功");
            }
            else
            {
              MessageBox.Show(ret.Info_DataMember);
            }
    
    
          }
          catch (Exception ex)
          { MessageBox.Show(ex.Message); }
    


     


    认真学习,不耻下问.^_^
    2011年7月12日 5:24

答案

  • Hi,

    由于SqlParameter(其实大多数数据访问组件除了DataSet,DataTable)是不可以序列化,对于这些组件,它们不可以用来设计为跨appdomain边界.我觉得你可以另外设计一个类。如:

        [DataContract]
        public class Hosipital
        {
     
           [DataMember]
            public string Paraname;
            [DataMember]
            public string Paravalue;
            [DataMember]
            public string Paratype;
           
     
        }

    将DataInfo改为:

    [DataContract]
      public class DataInfo
      {
        [DataMember]
        public string SQL { get; set; }
        [DataMember]
        public Hosipital[] paras { get; set; }
       
      }

    客户端调用时可以这样写:

                Hosipital[] sqlparams = new Hosipital[2];
     
                sqlparams[0] = new Hosipital();
                sqlparams[0].Paratype = "SqlDbType.NChar";
                sqlparams[0].Paraname = "@HospitalId";
                sqlparams[0].Paravalue = "100";
                
                sqlparams[1] = new Hosipital();
                sqlparams[1].Paratype = "SqlDbType.NVarChar";
                sqlparams[1].Paraname = "@Name";
                sqlparams[1].Paravalue = "100";

    你可以参考下面这两篇英文文章。

    http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-web-services/5193/Can-t-serialise-a-SqlCommand-over-WCF
    http://forums.asp.net/t/1487677.aspx


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
    2011年7月15日 2:40
    版主

全部回复

  • 再多问一句,,,我查了一下,好像只要有这个SqlParameter就不行呀,
    认真学习,不耻下问.^_^
    2011年7月12日 6:23
  • Hi,

    由于SqlParameter(其实大多数数据访问组件除了DataSet,DataTable)是不可以序列化,对于这些组件,它们不可以用来设计为跨appdomain边界.我觉得你可以另外设计一个类。如:

        [DataContract]
        public class Hosipital
        {
     
           [DataMember]
            public string Paraname;
            [DataMember]
            public string Paravalue;
            [DataMember]
            public string Paratype;
           
     
        }

    将DataInfo改为:

    [DataContract]
      public class DataInfo
      {
        [DataMember]
        public string SQL { get; set; }
        [DataMember]
        public Hosipital[] paras { get; set; }
       
      }

    客户端调用时可以这样写:

                Hosipital[] sqlparams = new Hosipital[2];
     
                sqlparams[0] = new Hosipital();
                sqlparams[0].Paratype = "SqlDbType.NChar";
                sqlparams[0].Paraname = "@HospitalId";
                sqlparams[0].Paravalue = "100";
                
                sqlparams[1] = new Hosipital();
                sqlparams[1].Paratype = "SqlDbType.NVarChar";
                sqlparams[1].Paraname = "@Name";
                sqlparams[1].Paravalue = "100";

    你可以参考下面这两篇英文文章。

    http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-web-services/5193/Can-t-serialise-a-SqlCommand-over-WCF
    http://forums.asp.net/t/1487677.aspx


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework
    2011年7月15日 2:40
    版主