using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.Serialization;namespace WCF_ANLEI
{
    [ServiceContract(Namespace = "")]
    public  interface XIEYI
    {
        
        [OperationContract]
        object object1();
  
    
    }    [DataContract]
    public class WCFserver : XIEYI
    {
 
      static ServiceHost servicehost;
        public void doListen()
        {
           
            XmlDictionaryReaderQuotas xm = new XmlDictionaryReaderQuotas();
            xm.MaxStringContentLength = 900000000;
            string adds = "http://127.0.0.1:9876/WCFlisten";
            Uri uu = new Uri(adds);
            servicehost = new ServiceHost(typeof(WCFserver), uu);
            WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
            ws.Security.Message.ClientCredentialType = MessageCredentialType.None;
            ws.TransactionFlow = true;
            ws.MaxReceivedMessageSize = 900000000;
            ws.ReaderQuotas = xm;
            try
            {
                servicehost.AddServiceEndpoint(typeof(XIEYI), ws, adds);
                ServiceMetadataBehavior be = new ServiceMetadataBehavior();
                be.HttpGetEnabled = true;
                servicehost.Description.Behaviors.Add(be);
                servicehost.Open();
                Console.WriteLine("WCF启动成功!");            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        
        
        }
        public void doclose()
        {
            servicehost.Close();
        
        
        }
        public object object1()
        {
            List<student> stu = new List<student>();
            student student1 = new student();
            student1.name = "张三";
            student1.password = "张三密码";
            student1.rid = "张三RID";            student student2 = new student();
            student2.name = "张三";
            student2.password = "张三密码";
            student2.rid = "张三RID";            stu.Add(student1);
            stu.Add(student2);
            return (object)stu;        }
    }
    [DataContract]
    public class student
    {
        [DataMember]
         public  string name;
        [DataMember]
         public string password;
        [DataMember]
         public string rid;
    
    }
    

解决方案 »

  1.   

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     此代码由工具生成。
    //     运行时版本:4.0.30319.1
    //
    //     对此文件的更改可能会导致不正确的行为,并且如果
    //     重新生成代码,这些更改将会丢失。
    // </auto-generated>
    //------------------------------------------------------------------------------[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="", ConfigurationName="XIEYI")]
    public interface XIEYI
    {
        
        [System.ServiceModel.OperationContractAttribute(Action="urn:XIEYI/Rid", ReplyAction="urn:XIEYI/RidResponse")]
        string Rid();
        
        [System.ServiceModel.OperationContractAttribute(Action="urn:XIEYI/object1", ReplyAction="urn:XIEYI/object1Response")]
        object object1();
    }[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface XIEYIChannel : XIEYI, System.ServiceModel.IClientChannel
    {
    }[System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class XIEYIClient : System.ServiceModel.ClientBase<XIEYI>, XIEYI
    {
        
        public XIEYIClient()
        {
        }
        
        public XIEYIClient(string endpointConfigurationName) : 
                base(endpointConfigurationName)
        {
        }
        
        public XIEYIClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public XIEYIClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public XIEYIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }
        
        public string Rid()
        {
            return base.Channel.Rid();
        }
        
        public object object1()
        {
            return base.Channel.object1();
        }
    }生成的客户端调用代码,并未将student类封装进去,求解,怎样将student封装进去,供客户端调用?
      

  2.   

    去掉你所有的attribute就可以了。看题目,我还真纳闷,除了用手写,还有人用脚写么?
      

  3.   

    去掉就好了?去掉就错的离谱了。
    这题目是可以吐槽下。因为你的方法中没有明确使用到student类,所以就没有封装进去(没试,我猜的,但的确有这种可能。)
    所谓没有明确使用到,就是你没用通过参数或者返回值去使用,而是函数内部使用,接口声明是体现不出来,那么系统根据接口的定义是不知道是否需要那个student类,此时你只要添加一个属性就OK。例如:
    [ServiceKnownType(typeof(student))]
    [OperationContract]
      object object1();
      

  4.   

    er...
    我的项目中就直接没任何attribute直接用的。咋没问题。
      

  5.   

    估计你的项目是直接引用的Dll文件,创建一个公共DLL,同时客户端和服务端引用,效率最高,我就是这样做的,但如果要让VS去识别服务端的接口信息,你不加属性它就认不到了。
      

  6.   

    感谢qldsrx的解答:
    封装成功:
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     此代码由工具生成。
    //     运行时版本:4.0.30319.1
    //
    //     对此文件的更改可能会导致不正确的行为,并且如果
    //     重新生成代码,这些更改将会丢失。
    // </auto-generated>
    //------------------------------------------------------------------------------namespace WCF_ANLEI
    {
        using System.Runtime.Serialization;
        
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="student", Namespace="http://schemas.datacontract.org/2004/07/WCF_ANLEI")]
        public partial class student : object, System.Runtime.Serialization.IExtensibleDataObject
        {
            
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            private string nameField;
            
            private string passwordField;
            
            private string ridField;
            
            public System.Runtime.Serialization.ExtensionDataObject ExtensionData
            {
                get
                {
                    return this.extensionDataField;
                }
                set
                {
                    this.extensionDataField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string name
            {
                get
                {
                    return this.nameField;
                }
                set
                {
                    this.nameField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string password
            {
                get
                {
                    return this.passwordField;
                }
                set
                {
                    this.passwordField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string rid
            {
                get
                {
                    return this.ridField;
                }
                set
                {
                    this.ridField = value;
                }
            }
        }
    }
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="", ConfigurationName="XIEYI")]
    public interface XIEYI
    {
        
        [System.ServiceModel.OperationContractAttribute(Action="urn:XIEYI/object1", ReplyAction="urn:XIEYI/object1Response")]
        [System.ServiceModel.ServiceKnownTypeAttribute(typeof(WCF_ANLEI.student))]
        object object1();
    }[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public interface XIEYIChannel : XIEYI, System.ServiceModel.IClientChannel
    {
    }[System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
    public partial class XIEYIClient : System.ServiceModel.ClientBase<XIEYI>, XIEYI
    {
        
        public XIEYIClient()
        {
        }
        
        public XIEYIClient(string endpointConfigurationName) : 
                base(endpointConfigurationName)
        {
        }
        
        public XIEYIClient(string endpointConfigurationName, string remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public XIEYIClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(endpointConfigurationName, remoteAddress)
        {
        }
        
        public XIEYIClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress)
        {
        }
        
        public object object1()
        {
            return base.Channel.object1();
        }
    }