none
Error to deserialize The InnerException message was 'ValueType 'System.Int32' cannot be null RRS feed

  • Question

  •  I am getting this error sporadically (it happened all the time about 2 hours ago, but now I am not getting the error) when bring up K2 forms (that uses K2 SmartObjects that calls WCF Service).
    When I kept getting the error, it seems to be at different functions

       http://tempuri.org/:GetAccountDetailsForWorkflowResult
       http://tempuri.org/:GetNewAccountFormInitializationDataResult
     
    The InnerException message was always the same: 'ValueType 'System.Int32' cannot be null.'

        Error: The formatter threw an exception while trying to deserialize the    message: There was an error while trying to deserialize parameter http://tempuri.org/:GetAccountDetailsForWorkflowResult. The InnerException message was 'ValueType 'System.Int32' cannot be null.'. Please see InnerException for more details.

    Why did I get this error 2 sporadically for the same workflow/form, sometimes I get the error sometimes I don't get the error ?
    Also, we tested the same workflows and service on 2 testing environments, and it works with no error.

    Could it be a problem with the network or the server ?

    How do I find what variables gives the error The InnerException message was 'ValueType 'System.Int32' cannot be null.' ?

    We just deployed a new WCF service yesterday, and the only think that may be related with System.Int32 and null is:
    I added a new field in 1 of the class Sponsor.cs (Sponsor is a table in our database, we are using EntityFramework CodeFirst Migration).
    The new field is:
    public int? numFO { get; set; }

    Could it be causing the error ? But, why the error doesn't happen all the time ?
    Saturday, December 10, 2016 11:57 PM

All replies

  • Hi aujong,

    >>"The InnerException message was always the same: 'ValueType 'System.Int32' cannot be null.'"

    Whether the exception will throw depends on the message which was deserialized. For example, If your entity is like this.

    public class MyEntity
    {
        [Required]
        public int PropertyA { get; set; }
    }

    As you see, the type of PropertyA is int and it can't be null. But if the message doesn't provide the information of PropertyA or provide null for this property, the exception will occur. For example,

    <MyEnity>
      <PropertyA></PropertyA>
    </MyEntity>
    <MyEnity>
    </MyEntity>

    I suggest you validate the xml or json before deserilizer it or change the type of PropertyA to int?(Nullable<int>) which could accept null value.

    Best Regards
    Li Wang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 12, 2016 3:09 AM
  • Thank you.

    We found the problem.
    In one of our object AccountDetailsDto, FO used to be int, but I changed it for this deploy to be ?int.

    public class AccountDetailsDto
    {
        public int ?FO { get; set; }

    Our WCF service is located in 2 servers (for load balancing). When it was deployed on Friday, the service in 1 of the server was refreshed, but the service in the other server wasn't refreshed.
    So, yesterday night we refreshed the service in both servers. So far that seems to fix the issue.
    It makes sense why yesterday the problem was sporadic, sometimes it happened sometimes it didn't happen. When it worked, it must have hit the server that was refreshed on Friday. When it didn't work, it must have hit the server that wasn't refreshed. Also it makes sense why it worked on both of our test environments, because on test environments I always refreshed the service on both servers.

    According to this article: http://stackoverflow.com/questions/6677546/problem-with-linq-model-cant-assign-null-to-nullable-boolean

    The problem was caused by an out of date service reference on my client, so whilst the service had a nullable bool field, the proxy on the client was still expecting a regular bool field, hence the serialization exception.

    Make sure your service references in your client projects are up to date, my problem was caused by a conflict between the data contracts exposed by my service being different to those expected by the client.

    Also http://stackoverflow.com/questions/25168165/cant-return-null-system-boolean-cannot-be-null

    As Nullable<bool> field was introduced recently, reference on client side for my service wasn't updated. After updating the reference it worked.

    Monday, December 12, 2016 2:34 PM
  • Hi aujong,

    I am glad to know you solved this problem and thanks for sharing the solution.  please mark it as answer, it will be very beneficial for other community members who have the similar questions. 

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 13, 2016 1:03 AM
    Moderator