none
WCF - IsRequired is not working as expected. RRS feed

  • Question

  • Hi,

    I have created a data contract as follows

    ------------------

    Data contract:    

        [DataContract(Namespace = Constants.NAMESPACE)]
        public class SourceData
        {
            [DataMember(Name = "Str1", IsRequired = true, EmitDefaultValue = false)]
            public string Str1  {get; set;}
            [DataMember(Name = "Str2", IsRequired = true, EmitDefaultValue = false)]
            public string Str2 {get; set;}
            [DataMember(Name = "Str3", IsRequired = true, EmitDefaultValue = false)]
            public string Str3 {get; set;}
            [DataMember(Name = "Count", IsRequired = true, EmitDefaultValue = false)]
            public int Count {get; set;}
            [DataMember(Name = "Flag", IsRequired = true, EmitDefaultValue = false)]
            public bool Flag {get; set;}
        }

    OPerations Contract is :

            [OperationContract]
            string MergeStrings( SourceData sd);

    Service method is:

    public class TestService : ITestPDFService
        {
            public string MergeStrings( SourceData sd1)
            {
                try
                {
                    return (sd1.Str1 + sd1.Str2 + sd1.Str3  + sd1.Count.ToString() + sd1.Flag.ToString() );
                }

            } }

    Client Code:

          TestService p = new TestService();
                SourceData SD1 = new SourceData();
                SD1.Str1 = "string1";
                SD1.Str2 = "string2";

                string result = p.MergeStrings(SD1);
                Console.WriteLine(result);

    Result: string1string20false

    --------------------------

    However, from the client even if i didn't pass the values, it is not throwing any error.

    all strings are set to null and int is set to 0 and bool variable set to false by default and the same values are used the service method and method is successful.

    the wcf service is self-hosted as windows service.

    How do specify the required fields? and How do I check whether a parameter is passed or not to the service?

    Regards,

    Venkat


    • Edited by vrkorada Monday, March 16, 2015 8:52 AM
    Monday, March 16, 2015 7:42 AM

All replies

  • Hi vrkorada,

    I have tested your code, but I can not reproduce your issue, because it is working as expected. The following runtime exception is thrown when the data members(Str3/Count/Flag) have not been assigned a value during serialization.

    So please try to rebuild your project and please make sure that once you have made a change in the service code, you have updated the service reference in the client to get the latest result.

    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.


    Tuesday, March 17, 2015 3:06 AM
    Moderator
  • Hi Amy,

    I tried again and still I am not getting any error. 

    Here is the complete solution for your debugging.

     https://drive.google.com/file/d/0B_NR0uBOqDaWaVF2S1ltRkkwajg/view?usp=sharing

    Best Regards,

    Venkat

    Tuesday, March 17, 2015 12:38 PM
  • Hi vrkorada,

    I have tested your code, but I still can not reproduce your issue, I meet the runtime exception as following:

    Please try to clean your project and delete the services reference of the client to test it again.
     
    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, March 20, 2015 8:51 AM
    Moderator