C# Web Service params string array

Posted on 2006-06-20
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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()

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 16950639
interesting idea - I will check my dev environment in a little while...

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): http://www.codeproject.com/soap/RefAndOL.asp

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 1500 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 asp.net 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) 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.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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