locked
WCF service method return error when all property values are not provided on call RRS feed

  • Question

  • User-2103146910 posted

    Dear all,

    i am writing a wcf service method that is used to update an existing record. the table that will update in the database contain around 60 fields. the user want to pass only the properties (with values) that need to update through this service method. i wrote this wcf service in c# but the client may use it with any platform they want so the request and response needs to be in xml [a requirement].

    as parameter i have defined a class representing the table fields, each field have

    isrequired=false,

    emitdefaultvalue=false

    now i am using soapui to test this service call; as follow:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:brm="http://schemas.datacontract.org/2004/07/BRM.BusinessEntities">
       <soapenv:Header/>
       <soapenv:Body>
          <tem:Update>
             <!--Optional:-->
             <tem:entity>
    			<brm:Name>1</brm:Name>
    			<brm:age>42</brm:age>
    			<brm:CustomerType>605</brm:CustomerType>
    			<brm:address>abc</brm:address>
             </tem:entity>
          </tem:Update>
       </soapenv:Body>
    </soapenv:Envelope>
    
    

    and receiving the following error.

    The formatter threw an exception while trying to deserialize the message:

    *) when i pass three or four parameters that are defined in the class right after each other then record is updated successfully [when the fields are defined from top to bottom order]

    *) when i pass parameters that are defined in the class with random order (not up to down) then the same error is shown.

    what i want :

    1) is there a way to pass parameters (in any no. like 2,3,4 etc) (defined anywhrere in the class) such that wcf service could accept it and then record is updated easily after execution

    possibilities :

    1) do i have to pass all parameter values; (if some value is not require to change then pass any specific / default value and process in the service such that it dont require any change) like pass null for string and in service apply an if statement that if value is null then it does not require to change anymore

    please suggest me the right way

    with many thanks

    Thursday, September 12, 2019 5:39 PM

All replies

  • User475983607 posted

    Order is not a factor.  I imagine you have other issues going on but you've shared zero code.

    Anyway the data contract defines this behavior.  Keep in mind that types have default values.  For example, an int is 0.  A DateTime is also zero which in turn is 1/1/0001 12:00:00 AM.

    Anyway, the data contract attribute is used to control binding behavior.

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts

    Web API might be a better option.

    Thursday, September 12, 2019 6:53 PM