none
Two DataContracts with same name RRS feed

  • Question

  • Hi,

    I having two data contracts on service side as,

    [DataContract(Name = "TestInterchangeableContract1")]
        public class TestInterchangeableContract2
        
    [DataContract]
        public class TestInterchangeableContract1

    Also, I have 2 operation contracts on service side, one which uses TestInterchangeableContract2 and the other uses TestInterchangeableContract1. 

    After import on client side, since the 2 contracts have same name and same namespace, how does WCF determine which one to import? 

    Thanks!

    Sunday, June 8, 2014 12:52 PM

Answers

  • Hi,

    I have test in my side with the following:

     [ServiceContract]
        public interface IService1
        {
    
    
            [OperationContract]
            TestInterchangeableContract1 GetDataUsingDataContract1(TestInterchangeableContract1 composite);
            
            [OperationContract]
            TestInterchangeableContract2 GetDataUsingDataContract2(TestInterchangeableContract2 composite);
    
            // TODO: Add your service operations here
        }
    
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        [DataContract(Name = "TestInterchangeableContract1")]
        public class TestInterchangeableContract2
    
        {
    
            [DataMember]
            public string TestInterchangeableContract2id { get; set; }
           
        }
    
    
    
        [DataContract]
        public class TestInterchangeableContract1
        {
    
            [DataMember]
            public string TestInterchangeableContract1name { get; set; }
        }




    Then in the client side:

    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

    ServiceReference1.TestInterchangeableContract1 TestInterchangeableContract1 = new ServiceReference1.TestInterchangeableContract1();

    TestInterchangeableContract1.TestInterchangeableContract1name = "ss"; client.GetDataUsingDataContract1(TestInterchangeableContract1); Console.WriteLine(client.GetDataUsingDataContract1(TestInterchangeableContract1).TestInterchangeableContract1name); Console.ReadLine();


    From my test, we can not use the ServiceReference1.TestInterchangeableContract2, and the TestInterchangeableContract1 only has one parameter "TestInterchangeableContract1name", so it uses only use the second DataContract.

    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.



    Friday, June 20, 2014 10:13 AM
    Moderator

All replies

  • The simple thing for you to do is just come up with two different names and be done with it.
    Sunday, June 8, 2014 5:16 PM
  • Hi,

    Please try to use the different DataContract or DataMember (Name="") parameter in the attribute like the following:

    [DataContract(Name="DataContractName1")]
    public class TestInterchangeableContract2
    {
      [DataMember(Name="FirstName")]
      public string Name {get; set;}
    }


    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.



    Monday, June 9, 2014 9:27 AM
    Moderator
  • Thank you for your response.

    I can make things work, that's not a problem. Just wanted to know how WCF determines which definition to use if we have 2 data contracts with same name as shown above.

    Tuesday, June 10, 2014 12:58 PM
  • Thank you for your response.

    I can make things work, that's not a problem. Just wanted to know how WCF determines which definition to use if we have 2 data contracts with same name as shown above.

    Your WCF Service will compile successfully, but when you try to consume the WCF Service from client you will get error message. So none of the DataContract will be used.

    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Tuesday, June 10, 2014 7:00 PM
  • It does work Gaurav. It takes definition of "TestInterchangeableContract1".

    You can try defining same name over 2 data contracts using DataMember (Name=""). Let me know what do you get.

    Wednesday, June 11, 2014 4:45 AM
  • Hi,

    I have test in my side with the following:

     [ServiceContract]
        public interface IService1
        {
    
    
            [OperationContract]
            TestInterchangeableContract1 GetDataUsingDataContract1(TestInterchangeableContract1 composite);
            
            [OperationContract]
            TestInterchangeableContract2 GetDataUsingDataContract2(TestInterchangeableContract2 composite);
    
            // TODO: Add your service operations here
        }
    
    
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        [DataContract(Name = "TestInterchangeableContract1")]
        public class TestInterchangeableContract2
    
        {
    
            [DataMember]
            public string TestInterchangeableContract2id { get; set; }
           
        }
    
    
    
        [DataContract]
        public class TestInterchangeableContract1
        {
    
            [DataMember]
            public string TestInterchangeableContract1name { get; set; }
        }




    Then in the client side:

    ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();

    ServiceReference1.TestInterchangeableContract1 TestInterchangeableContract1 = new ServiceReference1.TestInterchangeableContract1();

    TestInterchangeableContract1.TestInterchangeableContract1name = "ss"; client.GetDataUsingDataContract1(TestInterchangeableContract1); Console.WriteLine(client.GetDataUsingDataContract1(TestInterchangeableContract1).TestInterchangeableContract1name); Console.ReadLine();


    From my test, we can not use the ServiceReference1.TestInterchangeableContract2, and the TestInterchangeableContract1 only has one parameter "TestInterchangeableContract1name", so it uses only use the second DataContract.

    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.



    Friday, June 20, 2014 10:13 AM
    Moderator