C# Web Service params string array

Posted on 2006-06-20
Last Modified: 2008-03-21
Hello experts,

I am building a web service in C# which has a large number of input parameters.  Most of them are not necessary except in specific scenarios, but I require users to provide all parameters because thats what the web service needs (for most of them, users just pass a blank string to fulfill the necessary requirements).  

What I would like to know is if it possible to pass FEWER parameters than the method requires (some sort of web service equivalent to Request.QueryParams("value")).  

If I were to be doing this in pure C#, I would just provide a number of method overloads such as:

public void Method1(string a) { }
public void Method1(string a, int b, bool c) { }
public void Method1(string a, int b, bool c, string d) { }

and so forth...

However, this is clearly not just pure C# since each web method needs a unique name and cannot provide overloads.  What I am wondering is if there is some equivalent to Request.QueryParams("value) in a web service so I could pass a variable number of parameters.  This way, I could check if the user provided the given parameter, and act accordingly.  

Is this at all possible?

Question by:TLevin10
  • 5
  • 4
LVL 15

Expert Comment

ID: 16949417
You can overload WebMethods by declaring unique descriptions to each of them...

Take a look here...


publix string TestMe(string g)
     return g;

public string TestMe()
     return TestMe("hello");

Author Comment

ID: 16950538
right, this I already know - this allows overload methods on the server side, but to the client, its 2 different methods (they see "OverLoaded" as the method name for "TestMe(String g)" and "Normal" as the method name for "TestMe()"...

I am trying to find a way to have the client only call one message, but pass different params each time?
LVL 15

Expert Comment

ID: 16950567
Hmm unfortunately I don't have my dev environment here but it can be done....


[WebMethod(Description:="Method 1")]
public string testme(string g)

[WebMethod(Description:="Method 2")]
public string testme()

Author Comment

ID: 16950639
interesting idea - I will check my dev environment in a little while...
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.


Author Comment

ID: 16951671
It seems that the .NET server will throw an error unless the MessageName is specified for the overloads, which results in the user "seeing" 2 methods...
LVL 15

Expert Comment

ID: 16951691
When you say the user sees 2 methods, is this when they are calling the webservice from inside their application or by browsing to the test web service page?

Author Comment

ID: 16951858
well, either way - when they are browsing the test web service page, they see two methods, and for instance if I set up a form on another page and use that to call the webmethod, I need to rediect them to different locations based on the parameters:

<form action="http://localhost/WebServiceTest/TestServices.asmx/Method1" method="post" target="_blank"> for one set of parameters, and,
<form action="http://localhost/WebServiceTest/TestServices.asmx/Method2" method="post" target="_blank"> for another set of parameters

as an example of when this might fail, is if there is a checkbox which represents a parameter on a web form:

<INPUT type="checkbox" value="True" name="isBoolean"> - This will send a parameter of 'true' for the 'isBoolean' parameter, but...
<INPUT type="checkbox" value="False" name="isBoolean"> - This will send a 'null' parameter to the web method <causing problems>

Really, there should be an overloaded method where the isBoolean property doesn't exist, but for the user to use this method, they would have to call it by a different name:

'http://localhost/WebServiceTest/TestServices.asmx/Method2' instead of

It seems this is an intrinsic business of Web Methods (they need to have unique names):

I am hoping that maybe I can find a way around it, but they sure don't make it easy!
LVL 15

Accepted Solution

GavinMannion earned 500 total points
ID: 16951892
The way you are using webservices you cannot do what you are trying to do...

The only way would be to hack it yourself, create a page called MyMethod, let them post to that. Depending on what you get you then forward it to the service and then redirect the response.

Web services also get consumed in code and if you are comsuming the service then the code I posted will create the overload you are looking for.

If you are using then I don't see why you are posting using forms to the web service... it doesn't really make any sense?

Author Comment

ID: 16953810
Well, I am not the only one using the service - in fact, I'm just the designer of the service - it will be consumed by a series of windows apps and web apps which I have no control over.  Basically, I am trying to make it as easy to reach as possible for those who are going to consume the service.

The only reason I offer the simple ASP.NET examples is that it is easy to see where things can go wrong...

However, I have made the decision to just force users to enter blank parameters if they don't plan on using them - at least this way, the users aren't searching out the correct overload method to call and they just have to pass all the parameters as blanks.  

In such a situation, I don't need the overload methods and since its inherent in WSDL not to allow overloading, I guess this question stands resolved that there is no correct way to overload web methods.

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now