Share via

Unable to cast object of type 'System.Collections.ArrayList' to type 'System.String[]' error in my WCF service

Anusree 0 Reputation points
2026-05-22T07:52:45.5266667+00:00

We have a WCF service that peforms exchange powershell commadlets and parses the responses into a required json format. Recently, we started seeing issues with User/Group commandlets where it is reporting the below issue. Unable to cast object of type 'System.Collections.ArrayList' to type 'System.String[]'.
And other issues like

Type 'System.Collections.ArrayList' with data contract name 'ArrayOfanyType:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.

Would like to understand what has changed in the powershell commandlets response that this issue started appearing suddenly. Is there any article or any change from Microsoft that suggests this change

Developer technologies | .NET | Other

1 answer

Sort by: Most helpful
  1. Varsha Dundigalla(INFOSYS LIMITED) 5,025 Reputation points Microsoft External Staff
    2026-05-22T08:20:25.45+00:00

    Thank you for reaching out.

    From the behavior you are seeing, it looks like the PowerShell cmdlet output is no longer being returned as a fixed string[], but instead as a dynamic collection (ArrayList). Because WCF uses a strict serializer, it expects the exact type defined in the contract, and this mismatch is causing the casting and “type not expected” errors.

    There isn’t any official Microsoft article that confirms a specific change in the cmdlet response format. However, PowerShell does not guarantee a fixed collection type, so depending on the scenario or updates, the output can sometimes vary (for example, returning an ArrayList instead of an array).

    To resolve this, the recommended approach is to handle the response more flexibly in your service. For example, you can normalize the output by converting the result to a string[] before serialization, or update your code to handle both ArrayList and string[] cases safely.

    Please let us know if you require any further assistance, we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer". So that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.