Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Web Service params string array

Posted on 2006-06-20
9
Medium Priority
?
7,419 Views
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?

Thanks
0
Comment
Question by:TLevin10
[X]
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
9 Comments
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16949417
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
 

Author Comment

by:TLevin10
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?
0
 
LVL 15

Expert Comment

by:GavinMannion
ID: 16950567
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Author Comment

by:TLevin10
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...
0
 
LVL 15

Expert Comment

by:GavinMannion
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?
0
 

Author Comment

by:TLevin10
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
'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
 
LVL 15

Accepted Solution

by:
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?
0
 

Author Comment

by:TLevin10
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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