首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

WCF端接收不到对象的值 求解。该怎么处理

2012-01-05 
WCF端接收不到对象的值 求解。。C# codeusing Systemusing System.Collections.Genericusing System.Linq

WCF端接收不到对象的值 求解。。

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.ServiceModel.Channels;using System.Runtime.Serialization;using System.ServiceModel.Description;namespace WCFPractice{    [DataContract(Name = "Data")]    public class ServiceData : IExtensibleDataObject    {        [DataMember(Name = "Info")]        private string info;        public string Info        {            get { return info; }            set { info = value; }        }        private ExtensionDataObject extensionData;        public ExtensionDataObject ExtensionData        {            get { return extensionData; }            set { extensionData = value; }        }    }    [DataContract(Name = "Data")]    public class ClientData : IExtensibleDataObject    {        [DataMember(Name = "Info")]        private string info;        public string extraInfo;        public string Info        {            get { return info; }            set { info = value; }        }        private ExtensionDataObject extensionData;        public ExtensionDataObject ExtensionData        {            get { return extensionData; }            set { extensionData = value; }        }    }    [ServiceContract(Name = "GO")]    public interface IService    {        [OperationContract(Name = "DispInfo")]        void MakeSense(string info);        [OperationContract(Name = "DispEntity")]        void MakeSense(ServiceData serviceData);    }    public class ServiceImplementation : IService    {        public void MakeSense(string info)        {            Console.WriteLine(info);        }        public void MakeSense(ServiceData serviceData)        {            Console.WriteLine("Info=" + serviceData.Info + "     Extra Data=" + serviceData.ExtensionData);        }    }    //Name = "GO"必须与服务端设置的值一样,这样做是为了能找到该服务    [ServiceContract(Name = "GO")]    public interface IClient    {        [OperationContract(Name = "DispInfo")]        void MakeSense(string info);        [OperationContract(Name = "DispEntity")]        void MakeSense(ClientData clientData);    }    class Program    {        static void Main(string[] args)        {            ServiceHost host = new ServiceHost(typeof(ServiceImplementation),                               new Uri("http://localhost:8080"));//Uri随便定义            host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "Make");//Make随便定义            host.Open();            ChannelFactory<IClient> factory = new ChannelFactory<IClient>(new BasicHttpBinding(),                                              new EndpointAddress("http://localhost:8080/Make"));//将以上两个地址组合即可            IClient proxy = factory.CreateChannel();            proxy.MakeSense("Let's Go");//能正常显示            ClientData clientData = new ClientData();            clientData.Info = "I am Info";            clientData.extraInfo = "I am extra info";            proxy.MakeSense(clientData);//不能将clientData的值传送到WCF端。。            ((IChannel)proxy).Close();            host.Close();            Console.ReadKey();        }    }}


RT,proxy.MakeSense(clientData)执行的时候,当调用ServiceImplementation类的MakeSense时serviceData参数为null,不知为何没把clientData的值传递过去,很是无语。。。求高手解答。

[解决办法]
[DataMember]应该定义在每个Property的上面,而不是在private Field上。

热点排行