Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7466
  • Last Modified:

C# Web Service params string array

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?

Thanks
0
TLevin10
Asked:
TLevin10
  • 5
  • 4
1 Solution
 
GavinMannionCommented:
You can overload WebMethods by declaring unique descriptions to each of them...

Take a look here...

http://www.dotnetbips.com/articles/displayarticle.aspx?id=40

Simplified....

[WebMethod(MessageName="OverLoaded")]
publix string TestMe(string g)
{
     return g;
}

[WebMethod(MessageName="Normal")]
public string TestMe()
{
     return TestMe("hello");
}
0
 
TLevin10Author Commented:
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?
0
 
GavinMannionCommented:
Hmm unfortunately I don't have my dev environment here but it can be done....

Try

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

[WebMethod(Description:="Method 2")]
public string testme()
{
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TLevin10Author Commented:
interesting idea - I will check my dev environment in a little while...
0
 
TLevin10Author Commented:
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...
0
 
GavinMannionCommented:
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?
0
 
TLevin10Author Commented:
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
'http://localhost/WebServiceTest/TestServices.asmx/Method1'

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!
0
 
GavinMannionCommented:
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?
0
 
TLevin10Author Commented:
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.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now