3

I would like to pass some parameters to a web method as an array. The web method's signature does not have the params keyword.

I have a variable number of parameters (as the web method accepts) so I cannot put the array into n single variables.

How can this be done?

Christian Strempfer
7,3936 gold badges53 silver badges77 bronze badges
asked May 6, 2011 at 14:44

3 Answers 3

5

params is just syntactic sugar, why not just do something like this:

var myWebService = new MyWebService();
myWebService.MyMethod(new string[] { "one", "two", "three" });

The method signature on the web service side would just be:

public void MyMethod(string[] values);

If you post your web method maybe I can provide a better answer.

EDIT
If you can't modify the web method signature, then I would use an extension method to wrap the difficult to call web service. For example, if our web service proxy class looks like:

public class MyWebService
{
 public bool MyMethod(string a1, string a2, string a3, string a4, string a5,
 string a6, string a7, string a8, string a9, string a10)
 {
 //Do something
 return false;
 }
}

Then you could create an extension method that accepts the string array as params and makes the call to MyWebService.

public static class MyExtensionMethods
{
 public static bool MyMethod(this MyWebService svc, params string[] a)
 {
 //The code below assumes you can pass in null if the parameter
 //is not specified. If you have to pass in string.Empty or something
 //similar then initialize all elements in the p array before doing
 //the CopyTo
 if(a.Length > 10) 
 throw new ArgumentException("Cannot pass more than 10 parameters.");
 var p = new string[10];
 a.CopyTo(p, 0);
 return svc.MyMethod(p[0], p[1], p[2], p[3], p[4], p[5], 
 p[6], p[7], p[8], p[9]);
 }
}

You could then call your web service using the extension method you created (just be sure to add a using statment for the namespace where you declared you extension method):

var svc = new MyWebService();
svc.MyMethod("this", "is", "a", "test");
answered May 6, 2011 at 14:50

2 Comments

thanks for your answer. I cannot modify the web method and the method signature is WebMethod(string a1, string a2, ..., string a10)
@Francesco Got it, please see the update to my answer. Hope that helps.
1

Why don't you use..an array?

[WebMethod]
public string Foo(string[] values) 
{
 return string.Join(",", values);
}
answered May 6, 2011 at 14:49

4 Comments

I cannot modify the webmethod. Its signature is 'webmethod(string a1, string a2,..., string a10)'
it is not my style to downvote, I always appreciate the answers and for this reason I always vote up. I do not think it was me that downvoted but now I cannot even vote you up. I guess it is some issue related to the new version of Mozilla. As soon as it works properly I will vote you up.
@Francesco: no worries and no need to upvote since my answer didn't really help you to a solution, thanks for the clarification :)
my vote is locked :-( And anyway even if your reply is not useful for my question, I applied that Join instruction to another context so in some way it was useful to me. Thanks :-D
1

Ignore the fact that it is a web method for a moment. Ultimately, it is a method. And like all methods, is either defined to take parameters or not. If it is not defined to take parameters, then you can't pass parameters to it, can you? Not unless you have access to the source code and are able to chance it's definition.

If it is defined to take parameters, then the question is, is it defined to take array parameters or not? If not, then you can't pass parameters to it (not unless you can change it so that it can.)

answered May 6, 2011 at 15:34

2 Comments

thanks for your answer, it accepts parameters. Its signature is signature is 'webmethod(string a1, string a2,..., string a10)'. I was wondering wether I can pass an array (of 10 elements in this case) instad of passing each element
No, that wouldn't work, it would be nice if it was possible, though :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.