Search code examples
arrayswcfcode-analysisdatacontractdatamember

FXCop / Code Analysis on WCF DataContract Classes with Array - Creates CA1819 Error


I have the following WCF data contract class:

[DataContract]
public class BinaryResponse : ResponseBase
{
    [DataMember]
    public byte[] Payload { get; set; }
}

Nice and simple, works exactly as I need it to. However I am now running this through the full code analysis ruleset. This generates the following warning:

CA1819 : Microsoft.Performance : Change 'BinaryResponse.Payload' to return a collection or make it a method.

Having looked at the help page for this error the solution is a simple one. However the solution doesn't really fit with WCF datamembers.

So the question is, how can I refactor this class to still be useable as a WCF datacontract and also pass the code analysis?

Cheers


Solution

  • You can do change the byte array to an enumerable or collection, like tehe following:

    [DataContract]
    public class BinaryResponse : ResponseBase
    {
        [DataMember] public ICollection<byte> Payload { get; set; }
    }
    

    or you can keep the byte[] attribute in a private array and wrap it in an ICollection property (if you need the internal array):

    [DataContract]
    public class BinaryResponse : ResponseBase
    {
        // this is NOT a member of the DataContract
        private byte[] payload;
    
        [DataMember] public ICollection<byte> Payload {
            get { return this.payload; }
            set { this.payload = value.toArray(); }
        }
    }