0

I write a webmethod with optional parameter.

[WebMethod]
  public void EmailSend(string from, string to, string cc = null, string bcc = null, string replyToList = null, string subject = null, string body = null, bool isBodyHtml = false , string[] attachmentNames = null, byte[][] attachmentContents = null)
    {
     .....
    }

I call this method in client side application

 EmailServiceManagement.EmailService es = new EmailServiceManagement.EmailService();
 es.EmailSend(from, to,null,null,null,subject,body,true,attName,att); //this works

but

es.EmailSend(from,to); // this isn't working. According to c# optional parameter syntax it must work.

What am I doing wrong?

csjoseph
  • 159
  • 2
  • 20
  • 1
    Web methods should be platform/language independent and every language doesn't have to support optional parameters. So your first version is correct. – I4V May 21 '13 at 07:26
  • 2
    @I4V So It doesn't make any sense to use optional parameters in web methods. – csjoseph May 21 '13 at 07:30

1 Answers1

2

You can't have optional parameters on WebMethods. What you can do is have overloaded methods like this:

[WebMethod(MessageName="Test")]
public string GenerateMessage(string firstName)
{
   return string.Concat("Hi ", firstName);
}

[WebMethod(MessageName="AnotherTest")]
public string GenerateMessage(string firstName, string lastName)
{
   return string.Format("Hi {0} {1}", firstName, lastName);
}

Not sure exactly how you're interacting with this WebMethod but having so many parameters is probably an indication that you can group them in an object something like:

[WebMethod]
public void EmailSend(MessageParameters messageParams)
{
     .....
}
Dimitar Dimitrov
  • 14,868
  • 8
  • 51
  • 79