带有可选参数的WebMethod在客户端不起作用

时间:2013-05-21 07:24:12

标签: c# c#-4.0

我用可选参数编​​写了一个webmethod。

[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)
    {
     .....
    }

我在客户端应用程序中调用此方法

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您无法在WebMethods上拥有可选参数。你可以做的是有这样的重载方法:

[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);
}

不确定你是如何与这个WebMethod交互的,但是有这么多参数可能表明你可以将它们分组在一个像这样的对象中:

[WebMethod]
public void EmailSend(MessageParameters messageParams)
{
     .....
}