none
Reusing IEnumerable type RRS feed

  • Question

  • Hi,

    I have a problem when trying to reuse IEnumerable<KeyValuePair<string, object>> class. Every time I add service reference it creates new class inheriting System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, object>> and that causes ambiguous reference if I don't use full type qualifier.

    Generated class from Reference.cs is never used. What do I need to do to prevent class creation?

    These are data classes:

    namespace WcfReuseType.Data {
        [CollectionDataContract(Name = "DataDictionary")]
        public class DataDictionary : IEnumerable<KeyValuePair<string, object>> {
            private readonly IDictionary<string, object> _dict = new Dictionary<string, object>();
    
            public object this[string key] {
                get { return this._dict[key]; }
                set { this._dict[key] = value; }
            }
    
            public IEnumerator<KeyValuePair<string, object>> GetEnumerator() {
                return this._dict.GetEnumerator();
            }
    
            IEnumerator IEnumerable.GetEnumerator() {
                return this._dict.GetEnumerator();
            }
    
            public void Add(string key, object value) {
                this._dict.Add(key, value);
            }
            public void Add(KeyValuePair<string, object> item) {
                this._dict.Add(item);
            }
        }
    
        [DataContract]
        public class DataMessage {
            private DataDictionary _payload;
    
            public object this[string parameterName] {
                get { return this.Payload[parameterName]; }
                set { this.Payload[parameterName] = value; }
            }
    
            [DataMember]
            public DataDictionary Payload {
                get { return this._payload ?? (this._payload = new DataDictionary()); }
                set { this._payload = value; }
            }
    
            [DataMember]
            public string Name { get; set; }
    
            public DataMessage() { }
    
            public DataMessage(string name) {
                this.Name = name;
            }
        }
    }
    

    And service:

        [ServiceContract]
        public interface IService1 {
            [OperationContract]
            DataMessage GetMessage(DataMessage message);
        }
    
    
        public class Service1 : IService1 {
            public DataMessage GetMessage(DataMessage message) {
                message.Payload["response"] = DateTime.Now;
                return message;
            }
        }
    

    When I add service reference it always generates new class instead of reusing it.

        [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.CollectionDataContractAttribute(Name="DataDictionary", Namespace="http://schemas.datacontract.org/2004/07/WcfReuseType.Data", ItemName="KeyValuePairOfstringanyType")]
        [System.SerializableAttribute()]
        public class DataDictionary : System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, object>> {
        }
    

    DataMessage class is reused without any problems.

    Thank you!



    Tuesday, March 25, 2014 10:45 AM

All replies

  • Hi,

    If you're using the Visual Studio generated service references, you can pick what type is used for collections. then please right-click on the service under Service References and select Configure Service Reference, You should see these options:

    Configure Service Reference

    However, this still won't allow you to select IEnumerable<T>. If you want tighter control over what the client interface looks like, your best bet is to define the contracts in a separate assembly and then reference those assemblies on both the client and server.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, March 26, 2014 7:13 AM
    Moderator
  • Hi Amy,

    Thank you for your answer. I have defined contracts in different assembly in the WcfReuseType.Data namespace and referenced in both service and client, but client is unable to reuse DataDictionary class when adding service reference. It always generates new class as described above. DataMessage class is reused as expected.

    Is there some attribute that I'm missing or something else that would force class reusing?

    Thanks!

    Wednesday, March 26, 2014 9:01 PM