Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service Method with Optional Parameters [duplicate]

Possible Duplicate:
Can I have an optional parameter for an ASP.NET SOAP web service

I have tried looking around for this but I really cannot find the right solution for what I want to do. Sorry for the repetition since I know this has been asked before. I have a Web Service Method in which I want to have an optional parameter such as:

public void MyMethod(int a, int b, int c = -3) 

I already know I cannot use nullable parameters, so I am just trying to use a default value. Problem is when I call this method without supplying argument c is still get an exception. Is there somewhere I have to specify it is in fact optional?

Thanksю

like image 803
Steve Kiss Avatar asked Sep 27 '11 07:09

Steve Kiss


People also ask

Can we make Web API method parameters as optional?

Optional Parameters in Web API Attribute Routing and Default Values: You can make a URI parameter as optional by adding a question mark (“?”) to the route parameter. If you make a route parameter as optional then you must specify a default value by using parameter = value for the method parameter.

Can we have multiple optional parameters in C#?

By using Method Overloading: You can implement optional parameters concept by using method overloading. In method overloading, we create methods with the same name but with the different parameter list.

How do you make an optional argument in Ruby?

Flexible Methods With Optional Arguments You may want to make some of your arguments optional by providing a default value. Now you can call write with 2 arguments, in which case mode will equal the default value ( "w" ), or you can pass in 3 arguments to override the default value & get different results.

What is optional parameter function?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.


1 Answers

You can achieve optional parameters by overloads and using the MessageName property.

[WebMethod(MessageName = "MyMethodDefault")] public void MyMethod(int a, int b) {       MyMethod( a, b, -3); }  [WebMethod(MessageName = "MyMethod")] public void MyMethod(int a, int b, int c) 

For this to work, you may also need to change

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  

to

[WebServiceBinding(ConformsTo = WsiProfiles.None)]  

if you haven't done so already, as WS-I Basic Profile v1.1 does not allow overloads

like image 82
Markis Avatar answered Oct 09 '22 21:10

Markis