当前位置:编程学习 > C#/ASP.NET >>

wcf传递的参数为空,请帮忙看看

服务端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    [DataContract(Name = "MSG")]
    public class MSG
    {
        [DataMember(Name = "id")]
        public string id;

        [DataMember(Name = "name")]
        public string name;

        [DataMember(Name = "sex")]
        public int sex;

        [DataMember(Name = "demo")]
        public string demo;
    }

    [DataContract(Name = "RegMsg")]
    public class RegMsg
    {
        [DataMember(Name = "valueType")]
        public int valueType
        {
            get;
            set;
        }

        [DataMember(Name = "msg")]
        public string msg
        {
            get;
            set;
        }
    }

    [ServiceContract(SessionMode=SessionMode.Allowed)]
    public interface ITest
    {
        [OperationContract]
        List<string> GetList();

        [OperationContract]
        void Add(string lis);

        /// <summary>
        /// 根据id进行数据查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract(Name = "SelectMSG")]
        MSG SelectMSG(string id);

        /// <summary>
        /// 插入数据,进行注册
        /// </summary>
        /// <param name="msg"></param>
        [OperationContract(Name = "InsertMSG")]
        RegMsg InsertMSG(MSG msg);
    }

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
    public class Test : ITest,IDisposable
    {
        public static List<string> list = new List<string>();
        public List<string> GetList()
        {
            return list;
        }

        public void Add(string lis)
        {
            if(list.Contains(lis))
            {
                throw new Exception("重复");
            }
            list.Add(lis);
        }


        string connectString = @"server=ICBCOA-2BF62606\SQLEXPRESS;database=nsmc_53;user='sa';password='111658';";

        /// <summary>
        /// 进行注册
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public RegMsg InsertMSG(MSG msg)
        {
            RegMsg result=null;
            SqlConnection con = new SqlConnection(connectString);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "insertIDTB_proce";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            { 
                
                cmd.Parameters.Add("@id", SqlDbType.Char, 30);
                cmd.Parameters.Add("@name", SqlDbType.VarChar, 20);
                cmd.Parameters.Add("@sex", SqlDbType.Int);
                cmd.Parameters.Add("@demo", SqlDbType.VarChar, 200);
                cmd.Parameters.Add("@valueType", SqlDbType.Int);
                cmd.Parameters.Add("@msg", SqlDbType.VarChar, 10);
                //SqlParameter p1 = new SqlParameter("@valueType", SqlDbType.Int);
                //p1.Direction = ParameterDirection.Output;
                //SqlParameter p2 = new SqlParameter("@msg", SqlDbType.VarChar, 10);
                //p2.Direction = ParameterDirection.Output;
                cmd.Parameters["@valueType"].Direction = ParameterDirection.Output;
                cmd.Parameters["@msg"].Direction = ParameterDirection.Output;

                cmd.Parameters["@id"].Value = msg.id;
                cmd.Parameters["@name"].Value = msg.name;
                cmd.Parameters["@sex"].Value = msg.sex;
                cmd.Parameters["@demo"].Value = msg.demo;
                
                int reg = cmd.ExecuteNonQuery();
                if (reg < 1)
                    throw new Exception("接口异常");
                else
                {
                    result.valueType = Convert.ToInt32(cmd.Parameters["@valueType"].Value);
                    result.msg = cmd.Parameters["@msg"].Value.ToString();
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
            return result;
        }

        public MSG SelectMSG(string id)
        {
            string sessionid = OperationContext.Current.SessionId;
            MSG result = null;
            SqlConnection con = new SqlConnection(connectString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "selectIDTB_proce";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;
            try
            {
                con.Open();
                cmd.Parameters.Add("@id", SqlDbType.Char, 30);
                cmd.Parameters["@id"].Value = id;
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    result = new MSG();
                    result.name = dr["name"].ToString();
                    result.sex = Convert.ToInt32(dr["sex"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                con.Close();
            }
            return result;
        }

        #region IDisposable 成员

        public void Dispose()
        {
          //  list.Add("1单调");
            System.Threading.Thread.Sleep(60 * 1000);
        }

        #endregion
    }
}
客户端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using System.Collections;
using System.Xml;


namespace WindowsFormsApplication2
{
    [DataContract(Name = "MSG")]
    public class MSG
    {
        [DataMember(Name = "id")]
        public string id;

        [DataMember(Name="name")]
        public string name;

        [DataMember(Name="sex")]
        public int sex;

        [DataMember(Name = "demo")]
        public string demo;
    }

    [DataContract(Name="RegMsg")]
    public class RegMsg
    {
        [DataMember(Name = "valueType")]
        public int valueType
        {
            get;
            set;
        }

        [DataMember(Name = "msg")]
        public string msg
        {
            get;
            set;
        }
    }

    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        List<string> GetList();

        [OperationContract]
        void Add(string lis);

        /// <summary>
        /// 根据id进行数据查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [OperationContract(Name = "SelectMSG")]
        MSG SelectMSG(string id);

        /// <summary>
        /// 插入数据,进行注册
        /// </summary>
        /// <param name="msg"></param>
        [OperationContract(Name = "InsertMSG")]
        RegMsg InsertMSG(MSG msg);
    }

    public class Test : ClientBase<ITest>, ITest
    {
        public static List<string> list = new List<string>();
        public List<string> GetList()
        {
            return Channel.GetList();
        }

        public void Add(string lis)
        {
            Channel.Add(lis);
        }

        #region ITest 成员

        /// <summary>
        /// 查询接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MSG SelectMSG(string id)
        {
            MSG result=null;
            try
            {
                result = Channel.SelectMSG(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 插入接口
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public RegMsg InsertMSG(MSG msg)
        {
            RegMsg result = null;
            try
            {
                
                result = Channel.InsertMSG(msg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}

调用:
Test t = new Test();
t.Open();
result = t.InsertMSG(args);
MessageBox.Show(string.Format("result.valueType={0};result.msg={1}", result.valueType, result.msg));
t.Close();

当实体类作为参数的时候,联调的时候,不知为什么总是发现传进的参数到服务端为null,而返回值也是,在传递给客户端的时候都是为null,而string int等类型则能够正常传递。据说跟序列化有关,请看看是什么问题 --------------------编程问答-------------------- 实体的命名空间导致。你可以把MSG单独拿出来做一个程序集,服务端和客户端都引用这个程序集就好了 --------------------编程问答-------------------- 我的框架中有完美的WCF实现。

--------------------编程问答--------------------
引用 2 楼 chinahuyong 的回复:
我的框架中有完美的WCF实现。


=======================================================
.NET快速开发整合框架(RDIFramework.NET),基于.NET的快速开发、整合框架,给用户和开发者最佳的.Net框架部署方案。
平台博客:[CNBLOGS]http://www.cnblogs.com/huyong 
     [CSDN]http://blog.csdn.net/chinahuyong
交流QQ:406590790 (请注明:CSDN)
平台微博:http://t.qq.com/yonghu86
邮件交流:406590790@qq.com
--------------------编程问答-------------------- 你在数据契约上加上命名空间限定,例如[DataContract (NameSpace="http://schemas.zknu.edu.cn/Msg")]
服务端和客户端的数据契约都需要加
主要原因是:WCF在发布服务契约时默认命名空间即为官方默认地址,而数据契约的默认命名空间格式为官方默认地址+数据契约的命名空间,一般服务契约不出问题,而数据契约在服务端和客户端单独创建时就会出现命名空间不一致的情况,导致出问题。
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,