System.ArgumentException: Argument passed in is not serializable.
Tags: programming, net, c
Published: 2021-12-10 12:25:03

---
title: System.ArgumentException: Argument passed in is not serializable.
published: true
date: 2021-12-10 12:23:52 UTC
tags: programming,net,c
canonical_url: https://siderite.dev/blog/argument-passed-in-not-serializable/
---

Original post: https://siderite.dev/blog/argument-passed-in-not-serializable/

I got this exception at my work today, a System.ArgumentException with the message "_Argument passed in is not serializable._", that I could not quite understand. Where does it come from, since the .NET source repository does not contain the string? How can I fix it?

The stack trace ended up at _System.Collection.ListDictionaryInternal.set\_Item(Object key, Object value)_ in a method where, indeed, I was setting a value in a dictionary. But this is not how dictionaries behave! The dictionary in question was the Exception.Data property. It makes sense, because Exception objects are supposed to be serializable, and I was adding a value of type HttpMethod which, even if extremely simple and almost always used as an Enum, it is actually a class of its own which is not serializable!

So, there you have it, always make sure you add serializable objects in an exception's Data dictionary.

But why is this happening? The implementation of the Data property looks like this:

```
public virtual IDictionary Data { 
  [System.Security.SecuritySafeCritical]
  get {
    if (_data == null)
      if (IsImmutableAgileException(this))
        _data = new EmptyReadOnlyDictionaryInternal();
      else
        _data = new ListDictionaryInternal();
    return _data;
  }
}
```

Now, EmptyReadOnlyDictionaryInternal is just a dictionary you can't add to. The interesting class is [ListDictionaryInternal](https://referencesource.microsoft.com/#mscorlib/system/collections/listdictionaryinternal.cs "a simple implementation of IDictionary using a singly linked list. This will be smaller and faster than a Hashtable if the number of elements is 10 or less. This should not be used if performance is important for large numbers of elements."). Besides being an actual linked list implementation (who does that in anything but C++ classrooms?) it contains this code:

```
#if FEATURE_SERIALIZATION
  if (!key.GetType().IsSerializable)                 
    throw new ArgumentException(Environment.GetResourceString("Argument_NotSerializable"), "key");                    
  if( (value != null) && (!value.GetType().IsSerializable ) )
    throw new ArgumentException(Environment.GetResourceString("Argument_NotSerializable"), "value");                    
#endif
```

So **both key and value of the Data dictionary property in an Exception instance need to be serializable**.

But why didn't I find the string in the source reference? While the [Microsoft reference](https://referencesource.microsoft.com/ "Browse the .NET Framework source code online, with search and navigation powered by Roslyn.") website doesn't seem to support simple string search, it seems Google does NOT index the code GitHub pages either. You have to:

- manually go to GitHub and search
- get no results
- notice that the "Code" section of the results has a question mark instead of a number next to it
- click on it
- then it asks you to log in
- and only then you get results!

So bonus thing: **if you are searching for some string in the .NET source code, first of all use the [GitHub repo](https://github.com/dotnet ".NET Platform - Home of the open source .NET platform"), then make sure you log in** when you search.