Most likely there is sometimes coming data with unexpected type in the 'value' of the script function, and you try to cast it to IEntitySessionResult while it's actually a Color or string. You should just check the type is correct and if not do nothing. You can do it via the "as" casting like this:
var result = value as IEntitySessionResult;
if (result == null)
{