7

I have a web method with multiple parameters. The web method is only dependent on 2 fields, the rest are all 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 I declare these parameters as optional so that when I call the Web Method I only have to pass through the fields that i have values for, example:

WarehouseContactInformation(1,'Bill','00012311')
WarehouseContactInformation(1,'Bill','00012311','12415415','123525')
Alex
  • 8,827
  • 3
  • 42
  • 58
Jacques Bronkhorst
  • 1,685
  • 6
  • 34
  • 64
  • possible duplicate http://stackoverflow.com/questions/1723052/can-i-have-an-optional-parameter-for-an-asp-net-soap-web-service – शेखर Apr 03 '13 at 11:02

2 Answers2

12

You can't. Web methods doesn't support optional parameters. When you generate proxy for web method, you make get the specific signature, according to which you client and server would exchange the messages. But it can't pass the optional parameters. You can use default parameters on the server side, but no optional.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Alex
  • 8,827
  • 3
  • 42
  • 58
  • Do you have an example of these default params that I can use server Side? – Jacques Bronkhorst Apr 03 '13 at 11:03
  • 1
    Well, it like a standart default parameters in your classes: public string WarehouseContactInformation(int WAID, string CN , string CT = "defaultValue"). After this you proxi method will be generated as WarehouseContactInformation(int WAID, string CN) and each time you call it - it would call WarehouseContactInformation(int WAID, string CN , string CT = "defaultValue") on your service. – Alex Apr 03 '13 at 11:08
  • Thanks. You sir are a gentlemen And A scholar! – Jacques Bronkhorst Apr 03 '13 at 11:11
  • @voo Sir please check my answer posted below, am i doing wrong, if i doing this.Guide me.Thanks – Mogli Apr 03 '13 at 11:14
1

What i did is: send the parameter binded with XML, and don't bind the values of optional parameters leave that blank.

        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);

and in web service you can do like

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

Hope it helps.

Mogli
  • 1,972
  • 11
  • 34
  • 67
  • Yes, you can pass your parameters in XML, or JSon or even object, and this is a way to avoid optional parameters in this case. But symanticly it is more correct pass a part of data using methods to make the code more obvious. For example when I see the second part of your code, I don't understand what is the XmlNode. But if I change it to UpdateUser(string login, string pass, string role) - it becomes a bit clear and avoided of Xml parsing and unparsing – Alex Apr 03 '13 at 11:29