Search code examples
c#wcfstring.formatoperationcontractparams-keyword

WCF operation contract with params array parameter


I think this guy has the same problem as me, but I need clarification:

wcf service with params

Anyway, I'm trying to write a WCF operation contract that takes in a string the same way as String.Format does using a params array of objects, and a formatting string. That way, I can call the operation like this:

OperationContractMethod("Hello {0}", "Dave");

Instead of doing it like this:

OperationContractMethod(String.Format("Hello {0}", "Dave"));

Basically, the operation contract method takes a string, but I just want to be able to pass the string in formatted form for convenience. It works well with normal methods, because I can just create the function explicitly, but WCF presents the challenge that I have to write this in interface form first. The other guy said something about not all objects being serializable. Is it possible to do this or not?


Solution

  • No, I don't think it is possible. The data contract needs to be specific, and attributed, e.g.

    [DataContract]
    public class Contact
    {
        [DataMember]
        public string Name { get; set; }
    }