none
VS Service reference KnownTypeAttribute issue RRS feed

  • Question

  • Service description:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace WcfExample
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetData(int value);
    
            [OperationContract]
            CompositeType GetDataUsingDataContract(CompositeType composite);
    
            // TODO: Add your service operations here
        }
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfExample.ContractType".
        [DataContract]
        [System.Runtime.Serialization.KnownType(typeof(string[]))]
        [System.Runtime.Serialization.KnownType(typeof(TestClass1))]
        [System.Runtime.Serialization.KnownType(typeof(TestClass2))]
        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; }
            }
    
            [DataMember]
            public Dictionary<string, WrapedObject> Dict1
            {
                get;
                set;
            }
    
            [DataMember]
            public Dictionary<string, object> Dict2
            {
                get;
                set;
            }
    
            [DataMember]
            public Hashtable Dict3
            {
                get;
                set;
            }
        }
    
        [System.Runtime.Serialization.KnownType(typeof(string[]))]
        [System.Runtime.Serialization.KnownType(typeof(int[]))]
        public class WrapedObject
        {
            public object KnownTypeObject { get; set; }
        }
    
        public class TestClass1 { public string Name { get; set; } }
        public class TestClass2 { public string Name { get; set; } }
    }
    

    generated code by using add service reference:

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.18051
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    namespace ConsoleApplication1.ServiceReference1 {
        using System.Runtime.Serialization;
        using System;
        
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="CompositeType", Namespace="http://schemas.datacontract.org/2004/07/WcfExample")]
        [System.SerializableAttribute()]
        public partial class CompositeType : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
            
            [System.NonSerializedAttribute()]
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private bool BoolValueField;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private System.Collections.Generic.Dictionary<string, ConsoleApplication1.ServiceReference1.WrapedObject> Dict1Field;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private System.Collections.Generic.Dictionary<string, object> Dict2Field;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private System.Collections.Generic.Dictionary<object, object> Dict3Field;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private string StringValueField;
            
            [global::System.ComponentModel.BrowsableAttribute(false)]
            public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
                get {
                    return this.extensionDataField;
                }
                set {
                    this.extensionDataField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public bool BoolValue {
                get {
                    return this.BoolValueField;
                }
                set {
                    if ((this.BoolValueField.Equals(value) != true)) {
                        this.BoolValueField = value;
                        this.RaisePropertyChanged("BoolValue");
                    }
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public System.Collections.Generic.Dictionary<string, ConsoleApplication1.ServiceReference1.WrapedObject> Dict1 {
                get {
                    return this.Dict1Field;
                }
                set {
                    if ((object.ReferenceEquals(this.Dict1Field, value) != true)) {
                        this.Dict1Field = value;
                        this.RaisePropertyChanged("Dict1");
                    }
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public System.Collections.Generic.Dictionary<string, object> Dict2 {
                get {
                    return this.Dict2Field;
                }
                set {
                    if ((object.ReferenceEquals(this.Dict2Field, value) != true)) {
                        this.Dict2Field = value;
                        this.RaisePropertyChanged("Dict2");
                    }
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public System.Collections.Generic.Dictionary<object, object> Dict3 {
                get {
                    return this.Dict3Field;
                }
                set {
                    if ((object.ReferenceEquals(this.Dict3Field, value) != true)) {
                        this.Dict3Field = value;
                        this.RaisePropertyChanged("Dict3");
                    }
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public string StringValue {
                get {
                    return this.StringValueField;
                }
                set {
                    if ((object.ReferenceEquals(this.StringValueField, value) != true)) {
                        this.StringValueField = value;
                        this.RaisePropertyChanged("StringValue");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="WrapedObject", Namespace="http://schemas.datacontract.org/2004/07/WcfExample")]
        [System.SerializableAttribute()]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.CompositeType))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.TestClass1))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(ConsoleApplication1.ServiceReference1.TestClass2))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(System.Collections.Generic.Dictionary<string, ConsoleApplication1.ServiceReference1.WrapedObject>))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(int[]))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(string[]))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(System.Collections.Generic.Dictionary<string, object>))]
        [System.Runtime.Serialization.KnownTypeAttribute(typeof(System.Collections.Generic.Dictionary<object, object>))]
        public partial class WrapedObject : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
            
            [System.NonSerializedAttribute()]
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private object KnownTypeObjectField;
            
            [global::System.ComponentModel.BrowsableAttribute(false)]
            public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
                get {
                    return this.extensionDataField;
                }
                set {
                    this.extensionDataField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute()]
            public object KnownTypeObject {
                get {
                    return this.KnownTypeObjectField;
                }
                set {
                    if ((object.ReferenceEquals(this.KnownTypeObjectField, value) != true)) {
                        this.KnownTypeObjectField = value;
                        this.RaisePropertyChanged("KnownTypeObject");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="TestClass1", Namespace="http://schemas.datacontract.org/2004/07/WcfExample")]
        [System.SerializableAttribute()]
        public partial class TestClass1 : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
            
            [System.NonSerializedAttribute()]
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private string NameField;
            
            [global::System.ComponentModel.BrowsableAttribute(false)]
            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 {
                    if ((object.ReferenceEquals(this.NameField, value) != true)) {
                        this.NameField = value;
                        this.RaisePropertyChanged("Name");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="TestClass2", Namespace="http://schemas.datacontract.org/2004/07/WcfExample")]
        [System.SerializableAttribute()]
        public partial class TestClass2 : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
            
            [System.NonSerializedAttribute()]
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
            
            [System.Runtime.Serialization.OptionalFieldAttribute()]
            private string NameField;
            
            [global::System.ComponentModel.BrowsableAttribute(false)]
            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 {
                    if ((object.ReferenceEquals(this.NameField, value) != true)) {
                        this.NameField = value;
                        this.RaisePropertyChanged("Name");
                    }
                }
            }
            
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            
            protected void RaisePropertyChanged(string propertyName) {
                System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if ((propertyChanged != null)) {
                    propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
        }
        
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IService1")]
        public interface IService1 {
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
            string GetData(int value);
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetData", ReplyAction="http://tempuri.org/IService1/GetDataResponse")]
            System.Threading.Tasks.Task<string> GetDataAsync(int value);
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
            ConsoleApplication1.ServiceReference1.CompositeType GetDataUsingDataContract(ConsoleApplication1.ServiceReference1.CompositeType composite);
            
            [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService1/GetDataUsingDataContract", ReplyAction="http://tempuri.org/IService1/GetDataUsingDataContractResponse")]
            System.Threading.Tasks.Task<ConsoleApplication1.ServiceReference1.CompositeType> GetDataUsingDataContractAsync(ConsoleApplication1.ServiceReference1.CompositeType composite);
        }
        
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        public interface IService1Channel : ConsoleApplication1.ServiceReference1.IService1, System.ServiceModel.IClientChannel {
        }
        
        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
        public partial class Service1Client : System.ServiceModel.ClientBase<ConsoleApplication1.ServiceReference1.IService1>, ConsoleApplication1.ServiceReference1.IService1 {
            
            public Service1Client() {
            }
            
            public Service1Client(string endpointConfigurationName) : 
                    base(endpointConfigurationName) {
            }
            
            public Service1Client(string endpointConfigurationName, string remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress) {
            }
            
            public Service1Client(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(endpointConfigurationName, remoteAddress) {
            }
            
            public Service1Client(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                    base(binding, remoteAddress) {
            }
            
            public string GetData(int value) {
                return base.Channel.GetData(value);
            }
            
            public System.Threading.Tasks.Task<string> GetDataAsync(int value) {
                return base.Channel.GetDataAsync(value);
            }
            
            public ConsoleApplication1.ServiceReference1.CompositeType GetDataUsingDataContract(ConsoleApplication1.ServiceReference1.CompositeType composite) {
                return base.Channel.GetDataUsingDataContract(composite);
            }
            
            public System.Threading.Tasks.Task<ConsoleApplication1.ServiceReference1.CompositeType> GetDataUsingDataContractAsync(ConsoleApplication1.ServiceReference1.CompositeType composite) {
                return base.Channel.GetDataUsingDataContractAsync(composite);
            }
        }
    }
    

    All KnownTypeAttribute defined on CompositeType at service side, now generated on the WrapedObject class at client side.

    vs 2012 update 3, .net framework 4.5 project.you also can download the code from here: https://skydrive.live.com/redir?resid=8021851DA9291A38!4733&authkey=!AKR5CvwWtEF8syI


    快乐在于能够长时间的为自己认为值得的事情努力工作,不管它是什么。

    Wednesday, August 7, 2013 1:48 PM