How to pass optional parameters to a web method?

I have a web method with several parameters. The web method depends on only two fields, the rest are optional.

   [OperationContract]
    public string WarehouseContactInformation(int WAID (Required), string CN (Required), string CT (Optional), string CC (Optional), string CFN (Optional), string CD (Optional), string CE (Optional),string CW (Optional))

How to declare these parameters as optional so that when I call the Web method, I need to go through the fields for which I have values, for example:

WarehouseContactInformation(1,'Bill','00012311')
WarehouseContactInformation(1,'Bill','00012311','12415415','123525')
+5
source share
2 answers

You can not. Web methods do not support optional parameters. When you create proxi for the web method, you get a specific signature, according to which the client and server will exchange messages. But it cannot pass optional parameters. You can use the default settings on the server side, but not necessarily.

+10
source

: , XML, , .

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(
            "<registration>" +
            "<field1>" + value + "</field1>" +
            "<field2>" + value(or leave blank) + "</field2>" +
            "<field3>" + value + "</field3>" +
            "<field4>" + value + "</field4>" +
            "</registration>");

        int status = objectOfService.methodName(xmlDoc);

- ,

    public int UpdateUser(XmlNode node)
    {
       String filed1Value=node["field1"].InnerText;
    }

, .

+1

All Articles